-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_duplicates.py
43 lines (39 loc) · 1.28 KB
/
delete_duplicates.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
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
cur, pre, nh = head, None, None
count, tmp = 0, None
while cur is not None:
if tmp is None:
count, tmp = 1, cur
elif cur.val == tmp.val:
count += 1
else:
if count == 1:
tmp.next = None
if pre is None:
nh = pre = tmp
else:
pre.next = tmp
pre = tmp
if cur.next is None:
if pre is None:
nh = pre = cur
else:
pre.next = cur
else:
count, tmp = 1, cur
cur = cur.next
return nh
if __name__ == "__main__":
nh = Solution().deleteDuplicates(
ListNode(1, ListNode(2, ListNode(3, ListNode(3, ListNode(4, ListNode(4, ListNode(5))))))))
while nh is not None:
print(nh.val, end=" ")
nh = nh.next