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 ...
随机推荐
- dockerize 容器工具集基本使用
基本功能: * 在启动的时候根据环境变量或者模版生成配置文锦啊 * 多日志文件重定向到标准输入输出 * 等待其他服务(tcp,http unix)起来之后在启动主进程 1. 安装 直 ...
- gradle multiproject && docker build
备注: 环境准备 : docker , gradle(使用wrapper,或者全局安装),测试环境使用mac 1. gradle 安装 brew install gradle 2. docke ...
- iptables规则绑定在port而不是拦截在协议栈
版权声明:本文为博主原创,无版权.未经博主同意能够任意转载,无需注明出处,任意改动或保持可作为原创! https://blog.csdn.net/dog250/article/details/2417 ...
- 【转】刚发现一个linux在线文档库。很好很强大。
原文网址:http://blog.csdn.net/longxibendi/article/details/6048231 1.网址: http://www.mjmwired.net 2.比如查看这个 ...
- 研究ecmall一些流程、结构笔记 (转)
index.phpECMall::startup() //ecmall.php object //所有类的基础类 ecmall.phpBaseApp //控制器基础类 app.base.phpECBa ...
- Python学习笔记第一讲
1.pycharm快捷键 撤销与反撤销:Ctrl + z,Ctrl + Shift + z 缩进.不缩进:Tab.Shift + tab 运行:Shift + F10 取消注释,行注释:Ctrl + ...
- JEECG获取当前登录人的值
TSUser user = ResourceUtil.getSessionUserName(); mv.addObject("fbillerid", user.getUserNam ...
- JDK 9 & JDK 10 新特性
JDK 9 新增了不少特性,官方文档:https://docs.oracle.com/javase/9/whatsnew/toc.htm#JSNEW-GUID-527735CF-44E1-4144-9 ...
- Spring security 如何设置才能避免拦截到静态资源
问题:继承FilterSecurityInterceptor自定义Spring security拦截器,但是每次都拦截了css等静态资源,应该如何设置? @Override protected voi ...
- pull同步远程仓 笔记
一.远程仓库删除文件 远程仓 1.py 本地仓 1.py 2.py pull后 本地仓 1.py 这里的2.py 是没有改动过的情况,如改动了要解决冲突的,见:https://www.cnblogs ...