82. Remove Duplicates from Sorted List II (List)
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
.
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(!head) return head; ListNode *previous = NULL;
ListNode *current = head;
bool repeat = false;
while(current)
{
while(current->next && current->val == current->next->val)
{
current = current->next;
repeat = true;
}
if(repeat && previous!= NULL)
{
previous->next = current->next;
}
else if(!repeat)
{
if(!previous) head = current;
previous = current;
} current = current->next;
repeat = false;
}
if(!previous) return previous;
return head;
}
};
82. Remove Duplicates from Sorted List II (List)的更多相关文章
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- 82. Remove Duplicates from Sorted List II && i
题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 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 ...
- [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】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
- 82. Remove Duplicates from Sorted List II
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- leetcode 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- ecmall公告挂件分析(转)--此挂件写法已有更新的写法。
ecmall的首页,基本上都是由挂件的形式实现的.ecmall所有的挂件程序,都在external\widgets文件下面.ecmall首页公告的插件,就是notice目录里面. 分析里面文件,con ...
- angular先加载页面再执行事件,使用echarts渲染页面
剧情重现: 在一个页面中有多个小模块,这几个模块是可以拖动调顺序的,并且其中有两个模块使用了echarts渲染, 调整顺序angular插件有成熟的解决方案angular-sortable,https ...
- NanoPiM1开箱测试
等了快一周了,终于那M1与那外壳一起给我寄过来了. 上午收到,开箱图就不亮了,来一上好电的图! 一同购买来的MSD卡里什么也没有,上电测试时只看到绿色的灯微微亮(这是一个BUG吗!!!!哈哈). 所以 ...
- 1DAY 初识Python
一 本节目标 了解编程语言 了解python及与其他语言的优劣对比 安装python解释器及环境变量配置.运行python交互式环境 打印hello world程序 初识变量.用户输入,流程控制,wh ...
- C++中如何在顺序容器中删除符合特定条件的元素
以前很少做删除操作,vector一直当成数组用,而实际追求效率时又经常舍弃vector选用C风格数组.看<C++ Primer>到顺序容器删除这节时试着实现课后习题结果一动手我就出错了. ...
- Spring boot Freemarker 获取ContextPath的方法
Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...
- nginx限制请求之二:(ngx_http_limit_req_module)模块
相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...
- Java-Runoob:Java 数组
ylbtech-Java-Runoob:Java 数组 1.返回顶部 1. Java 数组 数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java 语言 ...
- laravel 环境自编译过程
[原创] 看到此文的朋友看完后也许会失望,但我尽最大努力不让搜友们失望,以下是自己操作的笔记用以整理提高 虽然 laravel 官方已给出了安装 laravel 框架所需的环境盒子 使用Vagrant ...
- 【UVALive】3029 City Game(悬线法)
题目 传送门:QWQ 分析 以前见到过差不多的这题. xhk说是单调栈水题,但我又不会单调栈,于是当时就放下了. 这么久过去了我还是不会用单调栈做这题,用的是悬线法. 非常好写 代码 #include ...