leetcode解题-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设定基本一样,要求返回值只包括唯一的元素。

分析

需要额外判断删除了重复值后剩下的数是本来就唯一呢,还是曾经有重复值。

代码

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

class (object):
def __init__(self, x):
self.val = x
self.next = None


class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""

dummy = ListNode(None)
dummy.next = head
prev = dummy
cur = head
while cur:
dup = False
while cur.next and cur.val == cur.next.val:
dup = True
cur = cur.next
# now cur is the either an unique number or the last one the a sequence of duplicates
if not dup:
prev.next = cur
prev = prev.next
cur = cur.next
prev.next = cur
return dummy.next