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. iOS 画虚线

    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )]; [self.view addSubvi ...

  2. 解决 IE6 背景缓存

    解决 IE6 背景缓存 <!--[if IE 6]><script type="text/javascript">document.execCommand( ...

  3. MVC 之 缓存机制(一)

    一.概述 缓存是将信息(数据或页面)放在内存中以避免频繁的数据库存储或执行整个页面的生命周期,直到缓存的信息过期或依赖变更才再次从数据库中读取数据或重新执行页面的生命周期.在系统优化过程中,缓存是比较 ...

  4. tail -f 和tail -F的区别

    http://flume.apache.org/FlumeUserGuide.html flume抓取 exec 的command 官网有如下建议:

  5. ACE中的参数截断工具-truncate

    变量截断工具是将类型A变量赋予类型B变量时使用,可自行判断变量是否需要截断,并且自动进行类型转换. 其全部为c实现 其入口为: ACE_Utils::truncate_cast<int> ...

  6. The Builder pattern simulates named optional parameters(Java)

    the Builder pattern is a good choice when designing classes whose constructors or static factories w ...

  7. 〖Linux〗Kubuntu设置打开应用时就只在打开时的工作区显示

    有没有遇到一种情况: 在工作区1打开了应用程序Google Chrome: 这个时间感觉它打开速度比较慢,就快捷键切换到工作区2了: 结果这个时候,Google Chrome就直接在工作区2打开,多不 ...

  8. 【Linux】文件权限

    Linux的每一个文件都跟多种类型相关联.在这些权限中,我们通常需要和三类权限打交道(用户.用户组以及其他实体). 1.文件权限查看ls –l Linux:/qinys # ls -l total 6 ...

  9. Spring Cloud Edgware SR3 让Zuul支持形如 /xxx和/xxx/yyy 格式的路径配置

    在包路径:org.springframework.cloud.netflix.zuul.filters 下,新建类SimpleRouteLocator,取代jar包中的类.内容如下: /* * Cop ...

  10. CentOS 6.4 安装 rabbitmq(3.6.15)

    安装废了一番周折,中间需要装一个socat,网上各种过时的地址. 无奈去socat官网,结果提供编译安装,编译安装完yum install rabbitmq的时候仍然提示缺少需要的依赖,烦,好在折腾两 ...