-
Notifications
You must be signed in to change notification settings - Fork 0
/
83.py
53 lines (46 loc) · 1.2 KB
/
83.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
# Definition for singly-linked list.
# add a new head
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Li(object):
def __init__(self, l):
self.head = ListNode(l[0])
cur = self.head
for i in range(1, len(l)):
cur.next = ListNode(l[i])
cur = cur.next
def returnHead(self):
return self.head
def printL(self, head):
cur = head
while cur:
print cur.val,'->',
cur = cur.next
class Solution(object):
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None:
return None
cur = head
repeat = False
while cur.next:
while cur.next and cur.next.val == cur.val:
cur.next = cur.next.next
repeat = True
if repeat == True:
repeat = False
else:
cur = cur.next
return head
if __name__ == '__main__':
solution = Solution()
li = Li([1,1,2])
li.printL(li.returnHead())
print
h = solution.deleteDuplicates(li.returnHead())
li.printL(h)