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. 使用visual studio code调试php代码

    这回使用visual studio code折腾php代码的调试,又是一顿折腾,无论如何都进不了断点.好在就要放弃使用visual studio code工具的时候,折腾好了,汗~ 这里把步骤记录下来 ...

  2. SQLServer中查询表结构(表主键 、列说明、列数据类型、所有表名)的Sql语句

    SQLServer中查询表结构(表主键 .列说明.列数据类型.所有表名)的Sql语句 1.查询数据库中的所有表名称: SELECT name FROM SysObjects Where XType=' ...

  3. Hessian 原理分析

    Hessian 原理分析 一.远程通讯协议的基本原理 网络通信需要做的就是将流从一台计算机传输到另外一台计算机,基于传输协议和网络 IO 来实现,其中传输协议比较出名的有 http . tcp . u ...

  4. debian下配置网络 安装无线网卡驱动 Broadcom BCMXX系列

    解决方案来自于debian官网  https://wiki.debian.org/wl 1.加入源 deb http://http.debian.net/debian/ wheezy main c ...

  5. MySQL 批量杀mysql线程

    mysql> SELECT concat('KILL ',id,';') FROM information_schema.processlist WHERE user='root'; +---- ...

  6. JDBC编程之预编译SQL与防注入式攻击以及PreparedStatement的使用教程

      转载请注明原文地址: http://www.cnblogs.com/ygj0930/p/5876951.html 在JDBC编程中,常用Statement.PreparedStatement 和  ...

  7. cocos2d-js Shader系列1:cocos2d-js Shader和OpenGL ES2.0

    cocos2d的Shader也就是差不多直接跟GPU打交道了,跟Flash的Stage3D(AGAL)类似,不过没有AGAL这么恶心,不需要直接编写汇编语言.而Fragment Shader又跟Fla ...

  8. uva 1658 Admiral (最小费最大流)

    uva 1658 Admiral 题目大意:在图中找出两条没有交集的线路,要求这两条线路的费用最小. 解题思路:还是拆点建图的问题. 首先每一个点都要拆成两个点.比如a点拆成a->a'.起点和终 ...

  9. django之异常错误

    现象:最近需要抓取一些网页的信息,但发现Python的乱码问题相对Java来说,不太一样.按照以往Java解决中文乱码问题的思路去解决Python乱码,貌似行不通,报错信息: SyntaxError: ...

  10. Windows下VS2017编译OpenCV 3.4.0-rc

    简述 很久没有用过OpenCV了,这次需要做一点图像处理相关的工作,又需要用起来,这里记录一下编译的过程.之前介绍过使用vs2015编译opencv2.4的帖子在这里. 编译好的文件在这里https: ...