forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-duplicates-from-sorted-list-ii.py
46 lines (41 loc) · 1.35 KB
/
remove-duplicates-from-sorted-list-ii.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Time: O(n)
# Space: O(1)
#
# 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.
#
# Definition for singly-linked list.
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:
# @param head, a ListNode
# @return a ListNode
def deleteDuplicates(self, head):
dummy = ListNode(0)
dummy.next = head
current = dummy
while current.next:
next = current.next
while next.next and next.next.val == next.val:
next = next.next
if current.next is not next:
current.next = next.next
else:
current = current.next
return dummy.next
if __name__ == "__main__":
head, head.next, head.next.next = ListNode(1), ListNode(2), ListNode(3)
head.next.next.next, head.next.next.next.next = ListNode(3), ListNode(4)
head.next.next.next.next.next, head.next.next.next.next.next.next = ListNode(4), ListNode(5)
print Solution().deleteDuplicates(head)