current position:Home>Leetcode-82 - delete duplicate Element II in the sorting linked list

Leetcode-82 - delete duplicate Element II in the sorting linked list

2022-04-29 18:18:00z754916067

subject

 Insert picture description here

Ideas

  1. Because the head node may be a duplicate node , So create a dummy node , And returns the name of the dummy node next As the final answer .
  2. And create a cur Mark where the current node goes .
  3. The judgment must be cur.next and cur.next.next,cur As the current marker , Never repeat .
  4. If there is a repetition , Because of the above bold reasons , It should be revised cur.next

Code

    public ListNode deleteDuplicates(ListNode head) {
    
        // Create an empty node as pre node  pre Of next Must return value 
        ListNode pre = new ListNode(0);
        pre.next=head;
        // Be sure to remember where the current node is 
        ListNode cur = pre;
        while (cur.next!=null && cur.next.next!=null){
    
            if(cur.next.val == cur.next.next.val){
    
                // Remember the current duplicate value 
                int x = cur.next.val;
                while(cur.next!=null && cur.next.val==x) cur.next = cur.next.next;

            }else cur = cur.next;
        }

        return pre.next;
    }

copyright notice
author[z754916067],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204291642292541.html

Random recommended