Skip to content

Commit 29cf626

Browse files
committed
feat: add solutions to leetcode problem: No.1836. Remove Duplicates From an Unsorted Linked List
1 parent 29fd5de commit 29cf626

File tree

6 files changed

+165
-13
lines changed

6 files changed

+165
-13
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<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>
33
</p>
44

55
<p align="center">
@@ -209,12 +209,12 @@
209209
1. 你也可以参考帮助文档「[GitHub](https://help.github.com/cn) / [Gitee](https://gitee.com/help)」了解更多细节。
210210

211211
<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>
213213
</p>
214214

215215
[Gitpod.io](https://www.gitpod.io) 是一个免费的在线开发环境,你也可以使用它参与本项目。
216216

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>
218218

219219
## 贡献者
220220

@@ -236,10 +236,10 @@
236236

237237
知名互联网科技博主 [@爱可可-爱生活](https://weibo.com/fly51fly) 微博推荐。
238238

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>
240240

241241
## 许可证
242242

243243
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享 版权归属-相同方式共享 4.0 国际 公共许可证</a>
244244

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>

README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<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>
33
</p>
44

55
<p align="center">
@@ -200,12 +200,12 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
200200
1. See [CONTRIBUTING](https://github.com/doocs/.github/blob/main/CONTRIBUTING.md) or [GitHub Help](https://help.github.com/en) for more details.
201201

202202
<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>
204204
</p>
205205

206206
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.
207207

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>
209209

210210
## Contributors
211211

@@ -227,4 +227,4 @@ Thank you to all our backers and sponsors!
227227

228228
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>.
229229

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>

solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,65 @@ None
1919
<!-- 这里可写当前语言的特殊实现逻辑 -->
2020

2121
```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
2344
```
2445

2546
### **Java**
2647

2748
<!-- 这里可写当前语言的特殊实现逻辑 -->
2849

2950
```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+
}
3181
```
3282

3383
### **...**

solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/README_EN.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,63 @@
6868
### **Python3**
6969

7070
```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
7293
```
7394

7495
### **Java**
7596

7697
```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+
}
78128
```
79129

80130
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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

0 commit comments

Comments
 (0)