File tree 6 files changed +165
-13
lines changed
solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List
6 files changed +165
-13
lines changed Original file line number Diff line number Diff line change 1
1
<p align =" center " >
2
- <a href =" https://github.com/doocs/leetcode " ><img src =" . /images/leetcode-doocs.png" alt =" LeetCode-GitHub-Doocs " ></a >
2
+ <a href =" https://github.com/doocs/leetcode " ><img src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main /images/leetcode-doocs.png" alt =" LeetCode-GitHub-Doocs " ></a >
3
3
</p >
4
4
5
5
<p align =" center " >
209
209
1 . 你也可以参考帮助文档「[ GitHub] ( https://help.github.com/cn ) / [ Gitee] ( https://gitee.com/help ) 」了解更多细节。
210
210
211
211
<p align =" center " >
212
- <a href =" https://github.com/doocs/leetcode " ><img src =" . /images/how-to-contribute.png" alt =" how-to-contribute " ></a >
212
+ <a href =" https://github.com/doocs/leetcode " ><img src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main /images/how-to-contribute.png" alt =" how-to-contribute " ></a >
213
213
</p >
214
214
215
215
[ Gitpod.io] ( https://www.gitpod.io ) 是一个免费的在线开发环境,你也可以使用它参与本项目。
216
216
217
- [ ![ Open in Gitpod] ( https://gitpod.io/button/open-in-gitpod.svg )] ( https://gitpod.io/#https://github.com/doocs/leetcode )
217
+ < a href = " https://gitpod.io/#https://github.com/doocs/leetcode " target = " _blank " alt = " Open in Gitpod " >< img src = " https://gitpod.io/button/open-in-gitpod.svg " ></ a >
218
218
219
219
## 贡献者
220
220
236
236
237
237
知名互联网科技博主 [ @爱可可-爱生活] ( https://weibo.com/fly51fly ) 微博推荐。
238
238
239
- [ ![ fly51fly] ( ./ images/recommender-fly51fly.png)] ( https://weibo.com/fly51fly )
239
+ < a href = " https://weibo.com/ fly51fly" target = " _blank " >< img src = " https://cdn.jsdelivr.net/gh/doocs/leetcode@main/ images/recommender-fly51fly.png" ></ a >
240
240
241
241
## 许可证
242
242
243
243
<a rel =" license " href =" http://creativecommons.org/licenses/by-sa/4.0/ " >知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a >
244
244
245
- [ ![ FOSSA Status ] ( https://app.fossa.com/api/ projects/git%2Bgithub.com%2Fdoocs%2Fleetcode.svg?type=large )] ( https://app.fossa.com/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode?ref=badge_large )
245
+ < a href = " https://app.fossa.com/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode?ref=badge_large " target = " _blank " >< img src = " https://app.fossa.com/api/ projects/git%2Bgithub.com%2Fdoocs%2Fleetcode.svg?type=large " ></ a >
Original file line number Diff line number Diff line change 1
1
<p align =" center " >
2
- <a href =" https://github.com/doocs/leetcode " ><img src =" . /images/leetcode-doocs.png" alt =" LeetCode-GitHub-Doocs " ></a >
2
+ <a href =" https://github.com/doocs/leetcode " ><img src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main /images/leetcode-doocs.png" alt =" LeetCode-GitHub-Doocs " ></a >
3
3
</p >
4
4
5
5
<p align =" center " >
@@ -200,12 +200,12 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
200
200
1 . See [ CONTRIBUTING] ( https://github.com/doocs/.github/blob/main/CONTRIBUTING.md ) or [ GitHub Help] ( https://help.github.com/en ) for more details.
201
201
202
202
<p align =" center " >
203
- <a href =" https://github.com/doocs/leetcode " ><img src =" ./ images/how-to-contribute.png" alt =" how-to- contribute" ></a >
203
+ <a href =" https://github.com/doocs/leetcode " ><img src =" https://cdn.jsdelivr.net/gh/doocs/leetcode@main/ images/how-to-contribute.png" alt =" How to contribute" ></a >
204
204
</p >
205
205
206
206
You can also contribute to [ doocs/leetcode] ( https://github.com/doocs/leetcode ) using [ Gitpod.io] ( https://www.gitpod.io ) , a free online dev environment with a single click.
207
207
208
- [ ![ Open in Gitpod] ( https://gitpod.io/button/open-in-gitpod.svg )] ( https://gitpod.io/#https://github.com/doocs/leetcode )
208
+ < a href = " https://gitpod.io/#https://github.com/doocs/leetcode " target = " _blank " alt = " Open in Gitpod " >< img src = " https://gitpod.io/button/open-in-gitpod.svg " ></ a >
209
209
210
210
## Contributors
211
211
@@ -227,4 +227,4 @@ Thank you to all our backers and sponsors!
227
227
228
228
This work is licensed under a <a rel =" license " href =" http://creativecommons.org/licenses/by-sa/4.0/ " >Creative Commons Attribution-ShareAlike 4.0 International License</a >.
229
229
230
- [ ![ FOSSA Status ] ( https://app.fossa.com/api/ projects/git%2Bgithub.com%2Fdoocs%2Fleetcode.svg?type=large )] ( https://app.fossa.com/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode?ref=badge_large )
230
+ < a href = " https://app.fossa.com/projects/git%2Bgithub.com%2Fdoocs%2Fleetcode?ref=badge_large " target = " _blank " >< img src = " https://app.fossa.com/api/ projects/git%2Bgithub.com%2Fdoocs%2Fleetcode.svg?type=large " ></ a >
Original file line number Diff line number Diff line change 19
19
<!-- 这里可写当前语言的特殊实现逻辑 -->
20
20
21
21
``` python
22
-
22
+ # Definition for singly-linked list.
23
+ # class ListNode:
24
+ # def __init__(self, val=0, next=None):
25
+ # self.val = val
26
+ # self.next = next
27
+ class Solution :
28
+ def deleteDuplicatesUnsorted (self , head : ListNode) -> ListNode:
29
+ cur = head
30
+ counter = collections.Counter()
31
+ while cur:
32
+ counter[cur.val] += 1
33
+ cur = cur.next
34
+
35
+ dummy = ListNode(0 , head)
36
+ pre, cur = dummy, head
37
+ while cur:
38
+ if counter[cur.val] > 1 :
39
+ pre.next = cur.next
40
+ else :
41
+ pre = cur
42
+ cur = cur.next
43
+ return dummy.next
23
44
```
24
45
25
46
### ** Java**
26
47
27
48
<!-- 这里可写当前语言的特殊实现逻辑 -->
28
49
29
50
``` java
30
-
51
+ /**
52
+ * Definition for singly-linked list.
53
+ * public class ListNode {
54
+ * int val;
55
+ * ListNode next;
56
+ * ListNode() {}
57
+ * ListNode(int val) { this.val = val; }
58
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
59
+ * }
60
+ */
61
+ class Solution {
62
+ public ListNode deleteDuplicatesUnsorted (ListNode head ) {
63
+ ListNode cur = head;
64
+ Map<Integer , Integer > counter = new HashMap<> ();
65
+ for (; cur != null ; cur = cur. next) {
66
+ counter. put(cur. val, counter. getOrDefault(cur. val, 0 ) + 1 );
67
+ }
68
+
69
+ ListNode dummy = new ListNode (0 , head);
70
+ ListNode pre = dummy;
71
+ for (cur = head; cur != null ; cur = cur. next) {
72
+ if (counter. get(cur. val) > 1 ) {
73
+ pre. next = cur. next;
74
+ } else {
75
+ pre = cur;
76
+ }
77
+ }
78
+ return dummy. next;
79
+ }
80
+ }
31
81
```
32
82
33
83
### ** ...**
Original file line number Diff line number Diff line change 68
68
### ** Python3**
69
69
70
70
``` python
71
-
71
+ # Definition for singly-linked list.
72
+ # class ListNode:
73
+ # def __init__(self, val=0, next=None):
74
+ # self.val = val
75
+ # self.next = next
76
+ class Solution :
77
+ def deleteDuplicatesUnsorted (self , head : ListNode) -> ListNode:
78
+ cur = head
79
+ counter = collections.Counter()
80
+ while cur:
81
+ counter[cur.val] += 1
82
+ cur = cur.next
83
+
84
+ dummy = ListNode(0 , head)
85
+ pre, cur = dummy, head
86
+ while cur:
87
+ if counter[cur.val] > 1 :
88
+ pre.next = cur.next
89
+ else :
90
+ pre = cur
91
+ cur = cur.next
92
+ return dummy.next
72
93
```
73
94
74
95
### ** Java**
75
96
76
97
``` java
77
-
98
+ /**
99
+ * Definition for singly-linked list.
100
+ * public class ListNode {
101
+ * int val;
102
+ * ListNode next;
103
+ * ListNode() {}
104
+ * ListNode(int val) { this.val = val; }
105
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
106
+ * }
107
+ */
108
+ class Solution {
109
+ public ListNode deleteDuplicatesUnsorted (ListNode head ) {
110
+ ListNode cur = head;
111
+ Map<Integer , Integer > counter = new HashMap<> ();
112
+ for (; cur != null ; cur = cur. next) {
113
+ counter. put(cur. val, counter. getOrDefault(cur. val, 0 ) + 1 );
114
+ }
115
+
116
+ ListNode dummy = new ListNode (0 , head);
117
+ ListNode pre = dummy;
118
+ for (cur = head; cur != null ; cur = cur. next) {
119
+ if (counter. get(cur. val) > 1 ) {
120
+ pre. next = cur. next;
121
+ } else {
122
+ pre = cur;
123
+ }
124
+ }
125
+ return dummy. next;
126
+ }
127
+ }
78
128
```
79
129
80
130
### ** ...**
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode() {}
7
+ * ListNode(int val) { this.val = val; }
8
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
+ * }
10
+ */
11
+ class Solution {
12
+ public ListNode deleteDuplicatesUnsorted (ListNode head ) {
13
+ ListNode cur = head ;
14
+ Map <Integer , Integer > counter = new HashMap <>();
15
+ for (; cur != null ; cur = cur .next ) {
16
+ counter .put (cur .val , counter .getOrDefault (cur .val , 0 ) + 1 );
17
+ }
18
+
19
+ ListNode dummy = new ListNode (0 , head );
20
+ ListNode pre = dummy ;
21
+ for (cur = head ; cur != null ; cur = cur .next ) {
22
+ if (counter .get (cur .val ) > 1 ) {
23
+ pre .next = cur .next ;
24
+ } else {
25
+ pre = cur ;
26
+ }
27
+ }
28
+ return dummy .next ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode:
3
+ # def __init__(self, val=0, next=None):
4
+ # self.val = val
5
+ # self.next = next
6
+ class Solution :
7
+ def deleteDuplicatesUnsorted (self , head : ListNode ) -> ListNode :
8
+ cur = head
9
+ counter = collections .Counter ()
10
+ while cur :
11
+ counter [cur .val ] += 1
12
+ cur = cur .next
13
+
14
+ dummy = ListNode (0 , head )
15
+ pre , cur = dummy , head
16
+ while cur :
17
+ if counter [cur .val ] > 1 :
18
+ pre .next = cur .next
19
+ else :
20
+ pre = cur
21
+ cur = cur .next
22
+ return dummy .next
You can’t perform that action at this time.
0 commit comments