[LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
这个题目跟[LeetCode] 83. Remove Duplicates from Sorted List_Easy tag: Linked List很类似,但是有可能要把head都去掉,所以还是要加上dummy node,并且使得dummy.next = head, 同时设置pre = dummy instead of None, 另外while loop判断条件为head and head.next, 最后返回dummy.next.
Code
class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
def removeDup2(self, head):
dummy = ListNode(0)
dummy.next = head
pre = dummy
while (pre.next and pre.next.next):
if pre.next.val == pre.next.next.val:
val = pre.next.val
while (pre.next and pre.next.val == val): # 把重复的点都删掉
pre.next = pre.next.next
else:
pre = pre.next
return dummy.next
2)
class Solution:
def removeDup2(self, head):
dummy = ListNode(0)
dummy.next = head
pre = dummy
while head and head.next:
if head.val == head.next.val:
val = head.val
while head and head.val == val:
pre = head.next
head.next = None
head = pre.next
else:
pre = head
head = head.next
return dummy.next
[LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List的更多相关文章
- [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 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
- 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 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
第一题:遍历链表,遇到重复节点就连接到下一个. public ListNode deleteDuplicates(ListNode head) { if (head==null||head.next= ...
- 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 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
随机推荐
- 帝国cms目录结构
/ 系统根目录├d/ 附件和数据存放目录 (data) │├file/ 附件存放目录 │├js/ JS调用生成目录 │└txt/ 内容存文本存放目录 ├ ...
- 通过phoenix创建hbase表失败,创建语句卡住,hbase-hmaster报错:exception=org.apache.hadoop.hbase.TableExistsException: SYNC_BUSINESS_INFO_BYDAY_EFFECT
问题描述: 前几天一个同事来说,通过phoenix创建表失败了,一直报表存在的错误,删除也报错,然后就针对这个问题找下解决方案. 问题分析: 1.通过phoenix创建表,一直卡住不动了.创建语句如下 ...
- 件测试专家分享III GUI自动化测试相关
GUI自动化:效率为王—脚本与数据解偶 页面对象模型的核心理念是,以页面(Web Page或者Native App Page)为单位来封装页面上的空间以及控件部分操作. 而测试用力,更确切的说是操作函 ...
- LinkedBlockingQueue(lbq)阻塞队列
最近开发中,经常使用这个类LinkedBlockingQueue,它是BlockingQueue这个子类. 并发库中的BlockingQueue是一个比较好玩的类,顾名思义,就是阻塞队列.该类主要提供 ...
- ch4-持久存储
1.处理数据和打印 man = [] other = [] try: data = open('sketch.txt') for each_line in data: try: (role, line ...
- 虚拟机 搭建LVS + DR + keepalived 高可用负载均衡
一:环境说明: LVS-DR-Master: 10.3.0.82 LVS-DR-Backup: 10.3.0.70 VIP: 10.3.0.60 ...
- Java环境变量中classpath是必须配置吗
设置环境变量在java 中需要设置三个环境变量(1.5之后不用再设置classpath了,但个人强烈建议继续设置以保证向下兼用问题)JDK安装完成之后我们来设置环境变量:右击“我的电脑”,选择“属性” ...
- Go基础---->go的基础学习(一)
这里面记录一些学习go的基础知识.我希望爱我的人不寂寞,我希望我爱的人喜欢我 go的基础知识 一.go中的map的使用 package main import ( "fmt" ) ...
- JQuery中$.ajax()方法参数详解 转载
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...
- NodeJS收发GET和POST请求
目录: 一 express框架接收 二 接收Get 三 发送Get 四 接收Post 五 发送Post 一 express框架接收 app.get('/',function(req,res) { va ...