82. Remove Duplicates from Sorted List II
来源:互联网 发布:网络中免费提供的商品 编辑:程序博客网 时间:2024/05/22 03:03
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
.
注意边界条件和循环的判断以及if-else的条件
添加dummy,简化情况。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* deleteDuplicates(ListNode* head) { if(!head) return NULL; ListNode *dummy = new ListNode(INT_MAX); dummy->next = head; ListNode *prev = dummy; ListNode *cur = head; ListNode *next = cur->next; while(cur) { if(next && cur->val != next->val) { prev->next = cur; prev = cur; cur = next; if(next) next = next->next; } else if(next && cur->val == next->val) { while(next && next->val == cur->val) { ListNode *nextNext = next->next; delete next; next = nextNext; } delete cur; cur = next; prev->next = cur; if(next) next = next->next; } else { cur = next; if(next) next = next->next; } } return dummy->next; }};
Remove Duplicates from Sorted List
题意要把链表中有重复的元素全部去除,只留下没有重复过的元素。
依旧是链表的操作,用pre和now去删除节点,由于头节点也有可能删除,因此先建一个Head节点连头节点。
pre从Head开始,用now去判断是否重复。
如果重复,循环到下一个不同元素之前,把之间的相同元素直接跳过。
再向前移动pre和now指针。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *deleteDuplicates(ListNode *head) { ListNode *pre,*now,*Head; if(!head||!head->next) return head; Head=new ListNode(-1); Head->next=head; pre=Head; now=head; while(now&&now->next) { if(now->val == now->next->val) { while(now->next && now->val == now->next->val) { now=now->next; } pre->next=now->next; now=now->next; } else { pre=now; now=now->next; } } head=Head->next; delete(Head); return head; }};
0 0
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 82. Remove Duplicates from Sorted List II
- 今天继续看不懂
- Tcl学习之--语法|变量
- Wireshark 抓包---诊断数据库
- 程序集清单(Assembly Manifest)的应用
- Tomcat源码阅读系列(五)Catalina容器
- 82. Remove Duplicates from Sorted List II
- 创业风云榜
- Java初级笔记_集合_2
- 数据总线DB(Data Bus)、地址总线AB(Address Bus)和控制总线CB(Control Bus)
- Asp.net mvc中controller与view间的如何传递数据
- Wireshark抓包:实例诊断TCP连接问题
- 基于情感词典的情感打分
- xcode 内存泄漏,memory leaks, crash 解决方案
- 分享好东西:android抓包工具fiddler使用介绍 抓取 手机APP 中资源。