82_RemoveDuplicatesfromSortedListII - a920604a/leetcode GitHub Wiki


title: 82. Remove Duplicates from Sorted List II tags:
- Linked List - Two Pointers categories: leetcode comments: false

problem

solution

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* slow = new ListNode(-101), *ret = slow;
        slow->next = head;
        while(slow->next){
            ListNode * fast = slow->next;
            while(fast->next && fast->next->val ==fast->val ) {
                fast=fast->next;
            }
            if(slow->next ==fast) slow=slow->next;
            else slow->next = fast->next;
        }
        return ret->next;
    }
};

analysis

  • time complexity O(n)
  • space complexity O(1)