File tree 4 files changed +97
-13
lines changed
lcci/02.01.Remove Duplicate Node
4 files changed +97
-13
lines changed Original file line number Diff line number Diff line change @@ -86,18 +86,16 @@ class Solution {
86
86
}
87
87
Set<Integer > s = new HashSet<> ();
88
88
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 ) {
91
91
if (! s. contains(p. val)) {
92
92
cur. next = p;
93
93
cur = cur. next;
94
94
s. add(p. val);
95
95
}
96
- p = p. next;
97
96
}
98
97
cur. next = null ;
99
98
return head;
100
-
101
99
}
102
100
}
103
101
```
@@ -134,6 +132,38 @@ class Solution {
134
132
};
135
133
```
136
134
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
+
137
167
### **...**
138
168
139
169
```
Original file line number Diff line number Diff line change 30
30
31
31
<ol >
32
32
<li>The length of the list is within the range[0, 20000].</li>
33
-
34
33
<li>The values of the list elements are within the range [0, 20000].</li>
35
34
36
35
</ol >
@@ -87,18 +86,16 @@ class Solution {
87
86
}
88
87
Set<Integer > s = new HashSet<> ();
89
88
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 ) {
92
91
if (! s. contains(p. val)) {
93
92
cur. next = p;
94
93
cur = cur. next;
95
94
s. add(p. val);
96
95
}
97
- p = p. next;
98
96
}
99
97
cur. next = null ;
100
98
return head;
101
-
102
99
}
103
100
}
104
101
```
@@ -135,6 +132,38 @@ class Solution {
135
132
};
136
133
```
137
134
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
+
138
167
### **...**
139
168
140
169
```
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -13,17 +13,15 @@ public ListNode removeDuplicateNodes(ListNode head) {
13
13
}
14
14
Set <Integer > s = new HashSet <>();
15
15
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 ) {
18
18
if (!s .contains (p .val )) {
19
19
cur .next = p ;
20
20
cur = cur .next ;
21
21
s .add (p .val );
22
22
}
23
- p = p .next ;
24
23
}
25
24
cur .next = null ;
26
25
return head ;
27
-
28
26
}
29
27
}
You can’t perform that action at this time.
0 commit comments