-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove_Duplicates_from_Sorted_List 2.py
92 lines (67 loc) · 1.64 KB
/
Remove_Duplicates_from_Sorted_List 2.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
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
class Solution:
# @param head, a ListNode
# @return a ListNode
def deleteDuplicates(self, head):
# base case
if head == None: return head
# its next link will hold the answer
dummy = ListNode(-1)
dummy.next = head
prev = dummy
current = head
while current:
# skip nodes having same value
while(current.next and current.val == current.next.val):
current = current.next
# no duplication
if prev.next == current:
prev = current
# duplication exist
else:
prev.next = current.next
# move to next node
current = current.next
return dummy.next
def printList(self, head, size=100):
pointer = head
s = ""
counter = 0
while pointer != None and counter < size:
s = s + str(pointer.val) + " , "
pointer = pointer.next
counter = counter + 1
print s
s = Solution()
head = ListNode(5)
pointer = head
for i in range(1,3,1):
temp = ListNode(1)
pointer.next = temp
pointer = pointer.next
for i in range(1,4,1):
temp = ListNode(2)
pointer.next = temp
pointer = pointer.next
for i in range(1,3,1):
temp = ListNode(3)
pointer.next = temp
pointer = pointer.next
for i in range(1,2,1):
temp = ListNode(6)
pointer.next = temp
pointer = pointer.next
s.printList(head)
x = s.deleteDuplicates(head)
s.printList(x)