File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ ###82 . Remove Duplicates from Sorted List II
2
+
3
+
4
+ 题目:
5
+
6
+ < https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ >
7
+
8
+
9
+ 难度:
10
+
11
+ Medium
12
+
13
+
14
+ 木有space 和 time的限制,第一想法,用dictionary存一下每个nodes的个数,这样只要看到它是大于1的,就删删删。
15
+
16
+ 虽然是笨办法。但是也可以AC
17
+
18
+ ```
19
+ class Solution(object):
20
+ def deleteDuplicates(self, head):
21
+ """
22
+ :type head: ListNode
23
+ :rtype: ListNode
24
+ """
25
+ dummy = ListNode(-1)
26
+ dummy.next = head
27
+
28
+ cur = dummy.next
29
+ nodeNumber = {}
30
+ while cur:
31
+ if cur.val in nodeNumber:
32
+ nodeNumber[cur.val] += 1
33
+ else:
34
+ nodeNumber[cur.val] = 1
35
+ cur = cur.next
36
+
37
+ cur = dummy
38
+ while cur.next:
39
+ if nodeNumber[cur.next.val] > 1:
40
+ cur.next = cur.next.next
41
+ else:
42
+ cur = cur.next
43
+ return dummy.next
44
+ ```
45
+
46
+
47
+ 谷歌一下,更省时间的方法是用一个prev 和 cur 指针,然后用一个bool来记录是否duplicate,这样loop一次即可解决问题。
You can’t perform that action at this time.
0 commit comments