Skip to content

Commit d2226b3

Browse files
committed
feat: update solutions to lcci problem: No.02.01. Remove Duplicate Node
1 parent bd9c3cb commit d2226b3

File tree

4 files changed

+97
-13
lines changed

4 files changed

+97
-13
lines changed

lcci/02.01.Remove Duplicate Node/README.md

+34-4
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,16 @@ class Solution {
8686
}
8787
Set<Integer> s = new HashSet<>();
8888
s.add(head.val);
89-
ListNode p = head.next, cur = head;
90-
while (p != null) {
89+
ListNode cur = head;
90+
for (ListNode p = head.next; p != null; p = p.next) {
9191
if (!s.contains(p.val)) {
9292
cur.next = p;
9393
cur = cur.next;
9494
s.add(p.val);
9595
}
96-
p = p.next;
9796
}
9897
cur.next = null;
9998
return head;
100-
10199
}
102100
}
103101
```
@@ -134,6 +132,38 @@ class Solution {
134132
};
135133
```
136134

135+
### **C++**
136+
137+
```cpp
138+
/**
139+
* Definition for singly-linked list.
140+
* struct ListNode {
141+
* int val;
142+
* ListNode *next;
143+
* ListNode(int x) : val(x), next(NULL) {}
144+
* };
145+
*/
146+
class Solution {
147+
public:
148+
ListNode* removeDuplicateNodes(ListNode* head) {
149+
if (head == nullptr || head->next == nullptr) {
150+
return head;
151+
}
152+
unordered_set<int> cache = {head->val};
153+
ListNode *cur = head;
154+
for (ListNode *p = head->next; p != nullptr; p = p->next) {
155+
if (!cache.count(p->val)) {
156+
cur->next = p;
157+
cur = cur->next;
158+
cache.insert(p->val);
159+
}
160+
}
161+
cur->next = nullptr;
162+
return head;
163+
}
164+
};
165+
```
166+
137167
### **...**
138168
139169
```

lcci/02.01.Remove Duplicate Node/README_EN.md

+34-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
<ol>
3232
<li>The length of the list is within the range[0, 20000].</li>
33-
3433
<li>The values of the list elements are within the range [0, 20000].</li>
3534

3635
</ol>
@@ -87,18 +86,16 @@ class Solution {
8786
}
8887
Set<Integer> s = new HashSet<>();
8988
s.add(head.val);
90-
ListNode p = head.next, cur = head;
91-
while (p != null) {
89+
ListNode cur = head;
90+
for (ListNode p = head.next; p != null; p = p.next) {
9291
if (!s.contains(p.val)) {
9392
cur.next = p;
9493
cur = cur.next;
9594
s.add(p.val);
9695
}
97-
p = p.next;
9896
}
9997
cur.next = null;
10098
return head;
101-
10299
}
103100
}
104101
```
@@ -135,6 +132,38 @@ class Solution {
135132
};
136133
```
137134

135+
### **C++**
136+
137+
```cpp
138+
/**
139+
* Definition for singly-linked list.
140+
* struct ListNode {
141+
* int val;
142+
* ListNode *next;
143+
* ListNode(int x) : val(x), next(NULL) {}
144+
* };
145+
*/
146+
class Solution {
147+
public:
148+
ListNode* removeDuplicateNodes(ListNode* head) {
149+
if (head == nullptr || head->next == nullptr) {
150+
return head;
151+
}
152+
unordered_set<int> cache = {head->val};
153+
ListNode *cur = head;
154+
for (ListNode *p = head->next; p != nullptr; p = p->next) {
155+
if (!cache.count(p->val)) {
156+
cur->next = p;
157+
cur = cur->next;
158+
cache.insert(p->val);
159+
}
160+
}
161+
cur->next = nullptr;
162+
return head;
163+
}
164+
};
165+
```
166+
138167
### **...**
139168
140169
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* removeDuplicateNodes(ListNode* head) {
12+
if (head == nullptr || head->next == nullptr) {
13+
return head;
14+
}
15+
unordered_set<int> cache = {head->val};
16+
ListNode *cur = head;
17+
for (ListNode *p = head->next; p != nullptr; p = p->next) {
18+
if (!cache.count(p->val)) {
19+
cur->next = p;
20+
cur = cur->next;
21+
cache.insert(p->val);
22+
}
23+
}
24+
cur->next = nullptr;
25+
return head;
26+
}
27+
};

lcci/02.01.Remove Duplicate Node/Solution.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ public ListNode removeDuplicateNodes(ListNode head) {
1313
}
1414
Set<Integer> s = new HashSet<>();
1515
s.add(head.val);
16-
ListNode p = head.next, cur = head;
17-
while (p != null) {
16+
ListNode cur = head;
17+
for (ListNode p = head.next; p != null; p = p.next) {
1818
if (!s.contains(p.val)) {
1919
cur.next = p;
2020
cur = cur.next;
2121
s.add(p.val);
2222
}
23-
p = p.next;
2423
}
2524
cur.next = null;
2625
return head;
27-
2826
}
2927
}

0 commit comments

Comments
 (0)