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. APUE 3rd

    以下是APUE 3rd edition 的preface,从04年的第二版到现在的第三版,APUE内容有所更新.点击下载. It’s been almost eight years since I fi ...

  2. javascript常用的公共方法

    附件下载 //摘要:将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式. //参数:复合格式字符串. //返回结果:format的副本,其中的格式项已替换为 args 中相应对象的字符串表 ...

  3. Velocity使用总结

    一.载入Velocity依赖包 <dependency> <groupId>org.apache.velocity</groupId> <artifactId ...

  4. Spring 基础概念——DI、IOC(一)

    一.IOC 控制反转 package com.qunar.studyspring.bean; import com.qunar.studyspring.dao.PersonDao; import co ...

  5. WEB打印控件Lodop使用体会

                    控件的使用方法,作者都已经有详细的使用说明供使用者参考. 但是对于打印表格,确实出现一点小问题,如果表格是自然高度,也就是只设置了table的高度,此时是可以正常显示的 ...

  6. docker overlay存储驱动介绍(传送门)

    https://blog.csdn.net/u010278923/article/details/79215828

  7. 转:Ogre的SceneManager分析

    SceneManager分析 场景管理主要工作包括以下几点: 1.可移动.不可移动和可渲染物体的创建删除. 2.场景查询. 3.渲染队列. 4.动态阴影. 一. 场景对象创建 场景中的所有对象,包括可 ...

  8. Python-Flask实现电影系统管理后台

    代码地址如下:http://www.demodashi.com/demo/14850.html 项目描述 该项目实现电影系统的后台接口,包括用户,电影,场次,订单,评论,优惠券,推荐,收藏等多个模块, ...

  9. Log4Net的应用教程之保存日志到数据库中

    关于Log4Net的应用,网上有很多教程,但大多数都是拷贝复制,有些按照他的代码来,运行起来发现也出不来效果,但是Log4net的作用实在是非常大的,或者这里说的不对,应该说系统的日志功能是很重要的也 ...

  10. shell脚本启动node服务

    #!/bin/bash cd /root/dev-web source /etc/profile /usr/local/node-8.11.1/bin/npm i && EGG_SER ...