当前位置:网站首页>[leetcode] delete duplicate Element II in the sorting linked list

[leetcode] delete duplicate Element II in the sorting linked list

2022-06-11 01:45:00 xiaoshijiu333

#LeetCode A daily topic 【 Special topic of linked list 】

  • Delete duplicate elements from the sort list II
    https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/

  • analysis
    Two ways of implementation : iteration + recursive
    iteration : Double pointer
    Normal thinking double pointer : Encountered the same slow pointer left as a marker , Come on, move on ;
    Meet different , Handle : Move the previous of the slow pointer next Set as fast;; So the key here is to find slow The last one of , The clever way is to use a dummy node
    Use the third pointer , Initially, it points to the dummy node , Then as the slow Forward , Keep in slow The one in front of ;

    The general process is shown above , It mainly deals with whether the slow pointer is in the last case , At the end of the last is not the same , If the last or the end is not the same, special treatment is required

  • Realization

public ListNode deleteDuplicates(ListNode head) {
    
        if (head == null) {
    
            return null;
        }
        ListNode tempNodeBegin = new ListNode(0, null);
        //  Add a dummy node 
        tempNodeBegin.next = head;
        ListNode slow = head, fast = head, slowTemp = tempNodeBegin;
        while (fast != null) {
    
            //  The values are equal ,fast Forward ,slow Stay to mark 
            if (slow.val != fast.val) {
    
                //  Not next to each other 
                if (slow.next != fast) {
    
                    slowTemp.next = fast;
                    slow = fast;
                } else {
    
                    slowTemp = slowTemp.next;
                    slow = slow.next;
                }
            }
            fast = fast.next;
        }
        // slow And fast Not next to ( That is, the end is the same number )
        if (slow.next != null) {
    
            slowTemp.next = null;
        }
        return tempNodeBegin.next;
    }

LeetCode Time consuming :0ms
 Insert picture description here

  • Achieve two : recursive
    Learn from recursion written by others
/*  Recursive processing  */
    public ListNode deleteDuplicates02(ListNode head) {
    
        if (head == null || head.next == null) {
    
            return head;
        }
        ListNode next = head.next;
        if (head.val == next.val) {
    
            //  Keep looking down , Until we encounter unequal val
            while (next != null && head.val == next.val) {
    
                next = next.next;
            }
            head = deleteDuplicates02(next);
        } else {
    
            ListNode node = deleteDuplicates02(next);
            head.next = node;
        }
        return head;
    }

LeetCode Time consuming :0ms
 Insert picture description here

  • summary
    About recursion :
    The logical processing before recursion is to process the incoming parameters
    The logical processing after recursion is to process the final result ( There are current and previous returned results )
    Recursive Trilogy :https://lyl0724.github.io/2020/01/25/1/
原网站

版权声明
本文为[xiaoshijiu333]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020623175509.html