[LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 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.
和83. Remove Duplicates from Sorted List 类似,这个题是有重复的元素就全部去掉,只保留独立的元素。
解法:去除重复元素的方法和83类似。但由于第一个元素有可能是重复的,如果删除了就不能往下接了,所以新建一个dummy, dummy.next指向链表。最后返回dummy.next就可以了。
Java:
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return head;
ListNode helper = new ListNode(0);
helper.next = head;
ListNode pre = helper;
ListNode cur = head;
while(cur!=null)
{
while(cur.next!=null && pre.next.val==cur.next.val)
{
cur = cur.next;
}
if(pre.next==cur)
{
pre = pre.next;
}
else
{
pre.next = cur.next;
}
cur = cur.next;
}
return helper.next;
}
}
Python:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None def __repr__(self):
if self is None:
return "Nil"
else:
return "{} -> {}".format(self.val, repr(self.next)) class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
dummy = ListNode(0)
pre, cur = dummy, head
while cur:
if cur.next and cur.next.val == cur.val:
val = cur.val;
while cur and cur.val == val:
cur = cur.next
pre.next = cur
else:
pre.next = cur
pre = cur
cur = cur.next
return dummy.next
C++:
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (!head || !head->next) return head;
ListNode *start = new ListNode(0);
start->next = head;
ListNode *pre = start;
while (pre->next) {
ListNode *cur = pre->next;
while (cur->next && cur->next->val == cur->val) cur = cur->next;
if (cur != pre->next) pre->next = cur->next;
else pre = pre->next;
}
return start->next;
}
};
类似题目:
[LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
All LeetCode Questions List 题目汇总
[LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II的更多相关文章
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- [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] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- [Leetcode] Remove duplicates from sorted list 从已排序的链表中删除重复元素
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 4.19——数组双指针——26. 删除有序数组中的重复项 & 27. 删除有序数组中的重复项II & 80. 删除有序数组中的重复项 II
第一次做到数组双指针的题目是80: 因为python的List是可以用以下代码来删除元素的: del List[index] 所以当时的我直接用了暴力删除第三个重复元素的做法,大概代码如下: n = ...
- [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项
2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...
- LeetCode 82,考察你的基本功,在有序链表中删除重复元素II
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...
随机推荐
- YES, There is No such thing as a free lunch
软件行业本身就建立在copy的基础上的,据说视窗both Windows and Mac OS都借鉴了施乐的. 国内的很多的软件质量真的好差呀. https://queue.acm.org/detai ...
- Codeforces C. Split a Number(贪心大数运算)
题目描述: time limit per test 2 seconds memory limit per test 512 megabytes input standard input output ...
- Integer面试连环炮以及源码分析
场景: 昨天有位朋友去面试,我问他面试问了哪些问题,其中问了Integer相关的问题,以下就是面试官问的问题,还有一些是我对此做了扩展. 问:两个new Integer 128相等吗? 答:不.因 ...
- 树莓派上 Docker 的安装和使用
Docker 是一个开源的应用容器引擎,可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不 ...
- less-3
首先来了解语句构造方法: 我们输入id=1可以看到未报错,输入id=1’报错,输入id=1’’未报错. 再对比下之前我们在less-1中的报错信息(如下图),可以看到,在less-3中报错信息中“1” ...
- luoguP1120小木棍(POJ - 1011 )
题意: 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50,个数不超过65. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段 ...
- YII2 更新数据不成功
起因: CLI模式,定时任务.同步其他系统中的DB数据,通过视图的方式. 历程: 原脚本已经写好,实在已经有的基础上修改,增加新的字段. 加了字段后,执行,但始终不成功,表里记录的utime也是能更新 ...
- shell脚本中向hive动态分区插入数据
在hive上建表与普通分区表创建方法一样: CREATE TABLE `dwa_m_user_association_circle`( `device_number` string, `oppo_nu ...
- modbus系列文章—汇总
请移步我博客园的网站 基本上是自己的原创,不是网上抄来抄去的,有很多干货,希望一边整理,一边修改-有不对的地方多多指教. https://www.cnblogs.com/CodeWorkerLiMin ...
- vue提示插件[vscode]
在VSCode Marketplace 搜素Vue 出现关于语法高亮的插件有 vue,vue-beautify,vue-color,VueHelper,vertur等等.比较了下载数量可以了解到,ve ...