LeetCode OJ:Remove Duplicates from Sorted List II(链表去重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
.
这里对于链表去重是要将有重复的链表节点全部都去掉,一个都不能留,思路还是比较简单的,跟前面的I那一题实际上差不多,代码如下所示:
/**
* 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 * helper = new ListNode(INT_MAX);
helper->next = head;
ListNode * prev = helper;
ListNode * curr = head;
while(curr){
if(curr->next && curr->val == curr->next->val){
ListNode * tmpNode = curr->next;
while(tmpNode->next && curr->val == tmpNode->next->val){
tmpNode = tmpNode->next;
}
prev->next = tmpNode->next;
curr = prev->next;
}else{
prev = curr;
curr = curr->next;
}
}
return helper->next;
}
};
LeetCode OJ:Remove Duplicates from Sorted List II(链表去重II)的更多相关文章
- [leetcode]83. Remove Duplicates from Sorted List有序链表去重
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode OJ——Remove Duplicates from Sorted List
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/ 链表的去重,要考虑链表的本质. #include <ios ...
- [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
随机推荐
- Django学习笔记之URL标签的使用
期初用django 开发应用的时候,完全是在urls.py 中硬编码配置地址,在views.py中HttpResponseRedirect()也是硬编码转向地址,当然在template 中也是一样了, ...
- [pixhawk笔记]11-Windows下PX4代码查看
由于项目需要做基于Simulink的PX4二次开发,在Windows下面做,所以需要在Windows下查看PX4的代码,故写该文档,记录环境安装和配置过程.按照该网页安装工具链:Windows Ins ...
- (转)国内yum源的安装(163,阿里云,epel)
国内yum源的安装(163,阿里云,epel) ----阿里云镜像源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS ...
- 20145312 《Java程序设计》第五周学习总结
20145312 <Java程序设计>第五周学习总结 学习笔记 Chapter8 异常处理 8.1语法与继承架构 1.Java中的错误以对象方式呈现,只要捕捉包装错误的对象,就可以针对该错 ...
- 2017阿里C++研发工程师-校招-单词匹配
题目描述 给一个字符串, 然后给一个字典. 把字符串分解成字典里的单词组成的句子, 请输出所需空格最少的方案.并输出该方案. 样例 例如: 字符串为: str="ilikealibaba&q ...
- Python遍历字典dict的几种方法
#!/usr/bin/python dict={"a":"apple","b":"banana","o&quo ...
- 关于JavaScript对象,你所不知道的事(二)- 再说属性
说完了对象那些不常用的冷知识,是时候来看看JavaScript中对象属性有哪些有意思的东西了. 不出你所料,对象属性自然也有其相应的特征属性,但是这个话题有点复杂,让我们先从简单的说起,对象属性的分类 ...
- Jenkins FreeStyle or Pipeline Job
FreeStyle Job: 1. 需要在页面添加模块配置项与参数完成配置 2. 每个Job仅能实现一个开发功能 3. 无法将配置代码化,不利于Job配置迁移与版本控制 4. 逻辑相对简单,无额外学习 ...
- creator cocos2d-js-min.js 文件廋身 变小 太大解决方法
使用的 cocos creator 1.2 版本, 菜单栏 项目 -- 项目设置 -- 模块设置 里面 把不要的模块去掉
- 如何打开windows的远程桌面
WINDOWS 2003/XP 系列设置方法 右键点击[我的电脑]选择[属性],进入[系统属性]界面,点击[远程]选项卡,勾选[启用这台计算机上的远程桌面]即可. 2003/XP系统默认只允许计算机a ...