LeetCode

Remove Duplicates from Sorted List II

2013.12.26 21:42

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

Solution1:

  This is a variation from the problem "Remove Duplicates from Sorted List". The difference is that you need to remove all nodes with duplicates, instead of removing the duplicates and keeping one in the list.

  So as to remove the current node, I used an extra pointer pointing to the previous node.

  This solution is still one-pass, with O(n) time complexity and O(1) space complexity.

Accepted code:

 1 // 1WA, 1AC, I thought there would be more mistakes... excellent
 2 /**
 3  * Definition for singly-linked list.
 4  * struct ListNode {
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int x) : val(x), next(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     ListNode *deleteDuplicates(ListNode *head) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         bool dup;
16         
17         if(head == nullptr){
18             return head;
19         }
20         
21         ListNode *root;
22         
23         root = new ListNode(0);
24         root->next = head;
25         ListNode *ptr = head;
26         ListNode *tmp;
27         ListNode *par = root;
28         
29         // 1WA here, initialization for $dup missing
30         dup = false;
31         while(ptr != nullptr){
32             if(ptr->next != nullptr){
33                 if(ptr->val == ptr->next->val){
34                     if(!dup){
35                         dup = true;
36                     }
37                     tmp = ptr->next;
38                     ptr->next = tmp->next;
39                     delete tmp;
40                 }else{
41                     if(dup){
42                         par->next = ptr->next;
43                         delete ptr;
44                         ptr = par->next;
45                         dup = false;
46                     }else{
47                         par = par->next;
48                         ptr = par->next;
49                     }
50                 }
51             }else{
52                 if(dup){
53                     par->next = ptr->next;
54                     delete ptr;
55                     ptr = par->next;
56                     dup = false;
57                 }else{
58                     par = par->next;
59                     ptr = par->next;
60                 }
61             }
62         }
63         
64         head = root->next;
65         delete root;
66         
67         return head;
68     }
69 };

Solution2:

  The previous solution has one new operation. It's better to avoid it with some extra code, if this function would be called frequently.

  If the head node has duplicates, we'll remove the leading duplicates and find a new head nodes first. The latter part is similar to the solution given above.

  It's still a one-pass algorithm with O(n) time complexity and O(1) space complexity.

Accepted code:

 1 // 1RE, 1AC, could've been perfect with more patience.
 2 /**
 3  * Definition for singly-linked list.
 4  * struct ListNode {
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int x) : val(x), next(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     ListNode *deleteDuplicates(ListNode *head) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         if(head == nullptr){
16             return head;
17         }
18         
19         int tmp_val = 0;
20         ListNode *par = nullptr, *ptr = nullptr;
21         // remove all head duplicates first
22         // 1RE here, {1} would result in $par uninitialized.
23         par = head;
24         while(head != nullptr && head->next != nullptr){
25             if(head->val == head->next->val){
26                 // head node has duplicates
27                 tmp_val = head->val;
28                 while(head != nullptr && head->val == tmp_val){
29                     ptr = head;
30                     head = head->next;
31                     delete ptr;
32                 }
33             }else{
34                 par = head;
35                 break;
36             }
37         }
38         
39         ListNode *ptr2 = nullptr;
40         ptr = par->next;
41         while(ptr != nullptr && ptr->next != nullptr){
42             if(ptr->val == ptr->next->val){
43                 tmp_val = ptr->val;
44                 while(ptr != nullptr && ptr->val == tmp_val){
45                     ptr2 = ptr;
46                     ptr = ptr->next;
47                     delete ptr2;
48                 }
49                 par->next = ptr;
50             }else{
51                 par = ptr;
52                 ptr = ptr->next;
53             }
54         }
55         
56         return head;
57     }
58 };
原文地址:https://www.cnblogs.com/zhuli19901106/p/3493159.html