zoukankan      html  css  js  c++  java
  • LeetCode | Remove Duplicates from Sorted List II

    https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

    不断定位出重复的段,然后删除这段。C++须要手动删除链表节点。

    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if (!head || !head->next) return head;
            
            ListNode dummy(0); dummy.next = head;
            ListNode *cur = &dummy, *start = head, *end = start; // start和end指针分别指向duplicates的开始和结束
            while (cur) {
                while (end && end->next && end->next->val == start->val) {
                    end = end->next;
                }
                if (end != start) {
                    cur->next = end->next;
                    // delete the duplicates
                    while (start != end) {
                        ListNode *next = start->next;
                        delete start;
                        start = next;
                    }
                    delete start;
                } else { cur = cur->next; }
                if (cur) { start = cur->next; end = start; }
            }
            return dummy.next;
        }
    };
    
  • 相关阅读:
    java8
    java8
    java8
    java8
    java8
    java8
    java8
    java8
    GUC-13 生产者和消费者案例-旧
    GUC-14 ForkJoin
  • 原文地址:https://www.cnblogs.com/ilovezyg/p/6379777.html
Copyright © 2011-2022 走看看