-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeleteDuplicates.py
More file actions
100 lines (89 loc) · 2.7 KB
/
DeleteDuplicates.py
File metadata and controls
100 lines (89 loc) · 2.7 KB
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
93
94
95
96
97
98
99
100
# 82. Remove Duplicates from Sorted List II
# 83. Remove Duplicates from Sorted List
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
# 递归调用:
# 当前node保留:
# head.next = delete(head.next)
# 当前node删除:
# head.val = delete(head.next).val
# head.next = delete(head.next)
class Solution:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
node = head
dict = {}
# 用hash table存放链表中值出现的次数
while node:
if node.val not in dict.keys():
dict[node.val] = 1
else:
dict[node.val] += 1
node = node.next
if not dict:
return head
else:
return self.delete(head, dict)
def delete(self, head, dict):
dummy = ListNode(None)
dummy.next = head
# head是尾节点时
if not head.next:
if dict[head] != 1:
return None
else:
return dummy.next
else:
# 删除head:
# head.val = delete(head.next).val
# head.next = delete(head.next)
if dict[head.val] != 1:
nextNode = self.delete(head.next, dict)
if nextNode:
head.val = nextNode.val
head.next = nextNode.next
else:
return None
else:
head.next = self.delete(head.next, dict)
return dummy.next
# 082解题(60ms)
class Solution2:
def deleteDuplicates(self, head):
dummy = ListNode(None)
dummy.next = head
cur = head
dict = {}
while cur: # 第一次遍历,统计链表中各元素出现的次数
val = cur.val
dict[val] = dict.get(val, 0) + 1
cur = cur.next
cur = dummy
while cur.next: # 第二次遍历,删除出现一次以上的元素
if dict[cur.next.val] > 1: # 若next元素被删除,则指针不动
cur.next = cur.next.next
else: # 反之则右移一位
cur = cur.next
return dummy.next
# 083的解题
class Solution3:
def deleteDuplicates(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
dummy = ListNode(None)
dummy.next = head
while head.next:
if head.val == head.next.val:
head.next = head.next.next
else:
head = head.next
return dummy.next