[InterviewBit] Remove Duplicates from Sorted List II

Remove Duplicates from Sorted List II

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* Solution::deleteDuplicates(ListNode* A) {
ListNode* dummy = new ListNode(INT_MIN);
dummy->next = A;
ListNode* runner = dummy;
while(runner->next and runner->next->next) {
if(runner->next->next->val == runner->next->val) {
ListNode* move = runner->next;
while(move and move->val == runner->next->val) move = move->next;
runner->next = move;
}
else runner = runner->next;
}
return dummy->next;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/14/PS/interviewbit/remove-duplicates-from-sorted-list-ii/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.