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 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)