current position:Home>Force deduction brush question 83 Delete duplicate elements in the sorting linked list

Force deduction brush question 83 Delete duplicate elements in the sorting linked list

2022-05-15 05:30:58GRT should keep working hard

 Insert picture description here

class ListNode {
    
    int val;
    ListNode next;
    ListNode() {
    }
    ListNode(int val) {
     this.val = val; }
    ListNode(int val, ListNode next) {
     this.val = val; this.next = next; }
  }
class Solution {
    
    public ListNode deleteDuplicates(ListNode head) {
    
          if (head == null){
    
            return head;
          }
          ListNode temp = head;
          while (temp.next != null){
    
              if (temp.val == temp.next.val){
    
                  temp.next = temp.next.next;
              }else {
    
                  temp = temp.next; // Be careful : Don't forget else, Otherwise, null pointer exception will be reported .
              }
          }
          return head;
    }
}

copyright notice
author[GRT should keep working hard],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/135/202205142234159747.html

Random recommended