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
.

非经常规的解法。为去除头节点的特殊性,须要用到虚拟头结点技术。

    ListNode *deleteDuplicates(ListNode *head) {
if (!head) return NULL; ListNode *dummyHead = new ListNode(0);
dummyHead->next = head; ListNode *prev = dummyHead, *curr = head->next;
int curVal = head->val;
while (curr) {
if (curr->val == curVal) {
for (curr = curr->next; curr != NULL && curr->val == curVal; curr = curr->next) ;
prev->next = curr;
if (curr) {
curVal = curr->val;
curr = curr->next;
}
}
else {
prev = prev->next;
curVal = curr->val;
curr = curr->next;
}
} return dummyHead->next;
}

LeetCode[Linked List]: Remove Duplicates from Sorted List II的更多相关文章

  1. 【LeetCode练习题】Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  2. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  3. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  4. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  5. LeetCode OJ 82. Remove Duplicates from Sorted List II

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

  6. 【LeetCode】080. Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  7. leetcode笔记:Remove Duplicates from Sorted Array II

    一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...

  8. LeetCode OJ 80. Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  9. leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. Android 之 WebView开发问题及优化

    WebView 在现在的项目中使用的频率应该还是非常高的,WebView 主要用来加载一些容易改变的频繁交互的应用App.目前 HTML5 是一种趋势.在开发中会遇到一些开发问题及优化问题,如下所记. ...

  2. vsphere脚本等

  3. Python取得系统进程列表

    一.上代码 import psutil for proc in psutil.process_iter(): try: pinfo = proc.as_dict(attrs=['pid', 'name ...

  4. python globals和locals

    文章里面说globals和locals函数返回的是命名空间 - 一个存有对应作用域的所有的变量.方法的字典,注意这里和dir函数返回数组的不一样 Python命名空间的本质 class Test(ob ...

  5. cache-control 缓存

    1.服务端设置 2. 3.所以一般设置css/js等静态文件加一个md5码. 4.优先级问题 如果服务器端同时设置了Etag和Expires 时,Etag原理同样,即与Last-Modified/Et ...

  6. window批处理杀死指定端口进程

    @echo off setlocal enabledelayedexpansion set /p port=请输入端口号: for /f "tokens=1-5" %%a in ( ...

  7. 如何删除Android studio中的注解代码

    http://blog.csdn.net/maimiho/article/details/52195081 先看下上面的文章.只是换下正则表达式即可 正则表达式:/\*[\s\S ]*

  8. cocos2d-js Shader系列3:多重纹理 multiple textures multiple samplers

    上一篇,我们学习了怎么便捷的控制sprite的颜色,而这个都是默认一个texture的,如果要实现类似mask的效果,或者更个性化的多纹理效果,怎么实现呢? 这就是这一节需要介绍的内容. 例如上图的效 ...

  9. python 测试时一个str是不是字符串

    # -*- coding: cp936 -*- #python 27 #xiaodeng #测试时一个str是不是字符串 def isAstring(obj): ''' 测试一个str是不是字符串 b ...

  10. Jenkins中集成jmeter-maven插件

    转自:http://my.oschina.net/u/1377774/blog/168969 目录[-] 第一步.先在maven工程中单独使用jmeter-maven插件 0.环境 1.在POM.xm ...