[Linked List]Remove Duplicates from Sorted List II
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.
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* help_node = new ListNode();
help_node->next = head;
ListNode* cur_pre = help_node;
ListNode* cur = head;
while(cur!=NULL){
ListNode* p = cur->next;
while(p!=NULL && p->val==cur->val){
p = p->next;
}
if(p != cur->next){
while(cur != p) {
ListNode* tmp = cur;
cur = cur->next;
delete (tmp);
}
cur_pre->next = p;
cur = p;
}else{
cur_pre = cur;
cur = cur->next;
}
}
head = help_node->next;
delete (help_node);
return head;
}
};
[Linked List]Remove Duplicates from Sorted List II的更多相关文章
- LeetCode[Linked List]: Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- 82. Remove Duplicates from Sorted List II && i
题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
随机推荐
- Identity 验证,Authorize 特性
多类型角色访问 //[Authorize] //[Authorize(Roles = "User")] //[Authorize(Roles="Administrator ...
- SqlServer2008数据库透明加密
前几天研究了一下sql数据库的透明加密,记下来加深一下理解. 用脚本创建文件夹 --查文件夹有没有 EXEC master.dbo.xp_fileexist 'D:\DATA\storedcerts' ...
- dojo.io.script
dojo.io.script 定义: 跨域访问数据,可以动态的将script标签插入到网页当中. 局限: 1.只支持get方式访问: 2.只支持异步调用. 使用: 1.dojo.io.script.g ...
- 【熊猫】POS销售
select a.itemcode,b.itemname,b.spec,b.unit,b.rprice,sum(a.rqty) rqtyfrom tm_possale_detail a,sys_ite ...
- UIViewController控制器的生命周期
视图控制器就是用来管理iOS程序中的视图,默认一个UIViewController为我们提供了一个视图UIView 我们称为根视图 - (instancetype)init{ if (self = ...
- 使用xib方式创建UITableViewCell,设置Label自动换行注意事项
自定义的UITableViewCell,使用xib方式创建,想要其中的UILabel换行显示:计算Label的高度,让其自动换行,总是没有效果. 我猜测原因可能在于使用了autolayout布局.只要 ...
- Java的动态代理机制详解(转)
在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...
- ENOVIA 基础
Part 零件 Part Master PM 如何表示零件:每当创建一个Part(给定一个Part Number),都回创建一个Part Master Part Master管理每个Part的最本质的 ...
- ZOJ问题--hdu3788
ZOJ问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- jquery $ dollar符号用法总结
参考:https://github.com/chyingp/blog/blob/master/jquery/jQuery%E6%BA%90%E7%A0%81-%E7%BE%8E%E5%85%83$%E ...