[LeetCode]题解(python):082 - Remove Duplicates from Sorted List II
题目来源
https://leetcode.com/problems/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
.
题意分析
Input:
:type head: ListNode
Output:
:rtype: ListNode
Conditions:与83题不同,只要元素出现过,则将该元素去掉
题目思路
因为list是有序的,并且可能返回一个空list,所以增加一个头节点。再增加一个节点时,就看这个节点之后是否有值与这个节点的值重复,如果有就不加这个值的【所有】节点
AC代码(Python)
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None:
return head ans = ListNode(-1)
ans.next = head
p = ans
temp = p.next while p.next:
while temp.next and temp.next.val == p.next.val:
temp = temp.next
if p.next == temp:
p = p.next
temp = p.next
else:
p.next = temp.next return ans.next
[LeetCode]题解(python):082 - Remove Duplicates from Sorted List II的更多相关文章
- LeetCode(80)Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- Java for LeetCode 082 Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 082 Remove Duplicates from Sorted List II 有序的链表删除重复的结点 II
给定一个有序的链表,删除所有有重复数字的节点,只保留原始列表中唯一的数字.例如:给定 1->2->3->3->4->4->5 ,则返回 1->2->5给 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
随机推荐
- 寒冰王座[HDU1248]
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- chrome inspect 远程调测:Chrome on Android之一 普通调试
本文PC环境: Chrome: 版本 33.0.1750.22 dev MAC OS:OS X 10.9.1 特别注意:Chrome DevToolsl使用时会联接到appspot.com,而此网址被 ...
- 【BZOJ】1103: [POI2007]大都市meg
http://www.lydsy.com/JudgeOnline/problem.php?id=1103 题意:一棵n节点的树(1<=n<=250000),m条边(1<=m<= ...
- 【UOJ】【UR #2】猪猪侠再战括号序列(splay/贪心)
http://uoj.ac/problem/31 纪念伟大的没有调出来的splay... 竟然那个find那里写错了!!!!!!!!!!!!! 以后要记住:一定要好好想过! (正解的话我就不写了,太简 ...
- BZOJ3676 [Apio2014]回文串
Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...
- 【C语言】06-基本数据类型
C语言有丰富的数据类型,因此它很适合用来编写数据库,如DB2.Oracle都是C语言写的. C语言的数据类型大致可以分为下图中的几类: 回到顶部 一.变量 跟其他语言一样,C语言中用变量来存储计算过程 ...
- Ubuntu SSH root user cannot login
Open /etc/ssh/sshd_config and check if PermitRootLogin is set to yes. If not, then set it to yes and ...
- GIT: 远程建立一个仓库,然后复制到本地
1. 登录 GIT,创建一个新的仓库 gitskills 2. 创建的时候,要选择 Initialize this repository with a readme ,让GitHub初始化仓库 3. ...
- Codeforces Round #357 (Div. 2) E 计算几何
传说中做cf不补题等于没做 于是第一次补...这次的cf没有做出来DE D题的描述神奇 到现在也没有看懂 于是只补了E 每次div2都是hack前2~3题 终于打出一次hack后的三题了...希望以后 ...
- A trip through the Graphics Pipeline 2011_04
Welcome back. Last part was about vertex shaders, with some coverage of GPU shader units in general. ...