LeetCode 82. Remove Duplicates from Sorted List II
1 class Solution { 2 public: 3 ListNode* deleteDuplicates(ListNode* head) { 4 if(head == NULL) return head; 5 ListNode* res = new ListNode(0); 6 res->next = head; 7 8 ListNode* prev = res; 9 10 while(head && head->next){ 11 if(head->val == head->next->val){ 12 while(head->next && head->val == head->next->val){ 13 ListNode* tmp = head->next; 14 head->next = head->next->next; 15 delete tmp; 16 } 17 ListNode* tmp = head; 18 prev->next = head->next; 19 head = head->next; 20 delete tmp; 21 } 22 else{ 23 prev = head; 24 head = head->next; 25 } 26 } 27 return res->next; 28 } 29 };