LeetCode OJ 82. Remove Duplicates from Sorted List II
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.
相对于Remove Duplicates from Sorted List这个问题,这个问题是要把重复出现过的所有元素全部删除。我的想法是找出每一个数字段的起点和终点,如果这个数据段出现了重复元素,就把这个数据段整体删除。
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null || head.next==null) return head;
ListNode thead = new ListNode(-1);
ListNode begin = thead;
ListNode end = head;
thead.next = head;
int cur = head.val;
while(end.next!=null){
if(end.next.val==cur){
end = end.next;
}
else{
if(begin.next==end){
begin = end;
end = end.next;
}
else{
begin.next = end.next;
end = begin.next;
}
cur = end.val;
}
}
if(begin.next==end) return thead.next;
begin.next = null;
return thead.next;
}
}
LeetCode OJ 82. Remove Duplicates from Sorted List II的更多相关文章
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ:Remove Duplicates from Sorted Array II(移除数组中的重复元素II)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【一天一道LeetCode】#82. Remove Duplicates from Sorted List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【Leetcode】82. Remove Duplicates from Sorted List II
Question: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dis ...
- LeetCode OJ:Remove Duplicates from Sorted List II(链表去重II)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 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 ...
随机推荐
- 在Ubuntu下搭建FTP服务器的方法
由于整个学校相当于一个大型局域网,相互之间传送数据非常快,比如要共享个电影,传点资料什么的. 所以我们可以选择搭建一个FTP服务器来共享文件. 那么问题来了,有的同学会问,我们既然在一个局域网内,直接 ...
- Design Pattern——单一职责原理
在类的职责分离上多考虑,做到单一职责,这样的代码才能做到易于维护,易扩展,灵活多样.
- "ApplicationDbContext"(泛指之类的数据库上下文模型)上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库。
一,在我使用自动生成数据库的时候,当你改变了数据库就会出现下面问题 "ApplicationDbContext"(泛指之类的数据库上下文模型)上下文的模型已在数据库创建后发生更改. ...
- 删除putty的session 以及 putty的颜色设置值
HKEY_CURRENT_USER\Software\SimonTatham\PuTTY 将sessions里面的内容清空即可 ==================================== ...
- Leetcode018 4Sum
public class S018 { //借鉴S015,速度有些慢 public List<List<Integer>> fourSum(int[] nums, int ta ...
- linode digitalocean哪个更好
大多数人纠结的品牌是Linode和DigitalOcean.我有幸使用过两者的产品,从2011年起,我就在用Linode VPS套餐,2013年开始,我订购了一批DigitalOcean产品,下面说下 ...
- 大数据加减(Big data addition and subtraction)
题目描述 Description 加减法是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(不超过1000位)的加减法算式,请给出正确结果.为提高速度,保证给定 ...
- 带宽 VS CDN (转载)
并发的影响因素:带宽.web server(含php).static server.数据库 带宽价格: 静态IP企业宽带 5M 10M 50M 100M 200M 盛大云 华东双线 216/月 396 ...
- ***参考Catch That Cow(BFS)
Catch That Cow Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- jq的siblings对a标签不起效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...