Skip to content

Commit 0da0027

Browse files
committed
feat: add solutions to lcci problem: No.02.01
No.02.01.Remove Duplicate Node
1 parent 0e26c96 commit 0da0027

File tree

4 files changed

+285
-0
lines changed

4 files changed

+285
-0
lines changed

lcci/02.01.Remove Duplicate Node/README.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,115 @@ func removeDuplicateNodes(head *ListNode) *ListNode {
186186
}
187187
```
188188

189+
### **TypeScript**
190+
191+
```ts
192+
/**
193+
* Definition for singly-linked list.
194+
* class ListNode {
195+
* val: number
196+
* next: ListNode | null
197+
* constructor(val?: number, next?: ListNode | null) {
198+
* this.val = (val===undefined ? 0 : val)
199+
* this.next = (next===undefined ? null : next)
200+
* }
201+
* }
202+
*/
203+
204+
function removeDuplicateNodes(head: ListNode | null): ListNode | null {
205+
if (head == null) {
206+
return head;
207+
}
208+
const set = new Set<number>([head.val]);
209+
let cur = head;
210+
while (cur.next != null) {
211+
if (set.has(cur.next.val)) {
212+
cur.next = cur.next.next;
213+
} else {
214+
set.add(cur.next.val);
215+
cur = cur.next;
216+
}
217+
}
218+
return head;
219+
}
220+
```
221+
222+
暴力(不推荐)
223+
224+
```ts
225+
/**
226+
* Definition for singly-linked list.
227+
* class ListNode {
228+
* val: number
229+
* next: ListNode | null
230+
* constructor(val?: number, next?: ListNode | null) {
231+
* this.val = (val===undefined ? 0 : val)
232+
* this.next = (next===undefined ? null : next)
233+
* }
234+
* }
235+
*/
236+
237+
function removeDuplicateNodes(head: ListNode | null): ListNode | null {
238+
let n1 = head
239+
while (n1 != null) {
240+
let n2 = n1
241+
while (n2.next != null) {
242+
if (n1.val === n2.next.val) {
243+
n2.next = n2.next.next;
244+
} else {
245+
n2 = n2.next;
246+
}
247+
}
248+
n1 = n1.next
249+
}
250+
return head
251+
};
252+
```
253+
254+
### **Rust**
255+
256+
```rust
257+
// Definition for singly-linked list.
258+
// #[derive(PartialEq, Eq, Clone, Debug)]
259+
// pub struct ListNode {
260+
// pub val: i32,
261+
// pub next: Option<Box<ListNode>>
262+
// }
263+
//
264+
// impl ListNode {
265+
// #[inline]
266+
// fn new(val: i32) -> Self {
267+
// ListNode {
268+
// next: None,
269+
// val
270+
// }
271+
// }
272+
// }
273+
use std::collections::HashSet;
274+
275+
impl Solution {
276+
pub fn remove_duplicate_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
277+
match head {
278+
None => head,
279+
Some(mut head) => {
280+
let mut set = HashSet::new();
281+
set.insert(head.val);
282+
let mut pre = &mut head;
283+
while let Some(cur) = &pre.next {
284+
if set.contains(&cur.val) {
285+
pre.next = pre.next.take().unwrap().next;
286+
} else {
287+
set.insert(cur.val);
288+
pre = pre.next.as_mut().unwrap();
289+
}
290+
}
291+
Some(head)
292+
}
293+
}
294+
}
295+
}
296+
```
297+
189298
### **...**
190299

191300
```

lcci/02.01.Remove Duplicate Node/README_EN.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,115 @@ func removeDuplicateNodes(head *ListNode) *ListNode {
186186
}
187187
```
188188

189+
### **TypeScript**
190+
191+
```ts
192+
/**
193+
* Definition for singly-linked list.
194+
* class ListNode {
195+
* val: number
196+
* next: ListNode | null
197+
* constructor(val?: number, next?: ListNode | null) {
198+
* this.val = (val===undefined ? 0 : val)
199+
* this.next = (next===undefined ? null : next)
200+
* }
201+
* }
202+
*/
203+
204+
function removeDuplicateNodes(head: ListNode | null): ListNode | null {
205+
if (head == null) {
206+
return head;
207+
}
208+
const set = new Set<number>([head.val]);
209+
let cur = head;
210+
while (cur.next != null) {
211+
if (set.has(cur.next.val)) {
212+
cur.next = cur.next.next;
213+
} else {
214+
set.add(cur.next.val);
215+
cur = cur.next;
216+
}
217+
}
218+
return head;
219+
}
220+
```
221+
222+
Violence (not recommended)
223+
224+
```ts
225+
/**
226+
* Definition for singly-linked list.
227+
* class ListNode {
228+
* val: number
229+
* next: ListNode | null
230+
* constructor(val?: number, next?: ListNode | null) {
231+
* this.val = (val===undefined ? 0 : val)
232+
* this.next = (next===undefined ? null : next)
233+
* }
234+
* }
235+
*/
236+
237+
function removeDuplicateNodes(head: ListNode | null): ListNode | null {
238+
let n1 = head
239+
while (n1 != null) {
240+
let n2 = n1
241+
while (n2.next != null) {
242+
if (n1.val === n2.next.val) {
243+
n2.next = n2.next.next;
244+
} else {
245+
n2 = n2.next;
246+
}
247+
}
248+
n1 = n1.next
249+
}
250+
return head
251+
};
252+
```
253+
254+
### **Rust**
255+
256+
```rust
257+
// Definition for singly-linked list.
258+
// #[derive(PartialEq, Eq, Clone, Debug)]
259+
// pub struct ListNode {
260+
// pub val: i32,
261+
// pub next: Option<Box<ListNode>>
262+
// }
263+
//
264+
// impl ListNode {
265+
// #[inline]
266+
// fn new(val: i32) -> Self {
267+
// ListNode {
268+
// next: None,
269+
// val
270+
// }
271+
// }
272+
// }
273+
use std::collections::HashSet;
274+
275+
impl Solution {
276+
pub fn remove_duplicate_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
277+
match head {
278+
None => head,
279+
Some(mut head) => {
280+
let mut set = HashSet::new();
281+
set.insert(head.val);
282+
let mut pre = &mut head;
283+
while let Some(cur) = &pre.next {
284+
if set.contains(&cur.val) {
285+
pre.next = pre.next.take().unwrap().next;
286+
} else {
287+
set.insert(cur.val);
288+
pre = pre.next.as_mut().unwrap();
289+
}
290+
}
291+
Some(head)
292+
}
293+
}
294+
}
295+
}
296+
```
297+
189298
### **...**
190299

191300
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
use std::collections::HashSet;
18+
19+
impl Solution {
20+
pub fn remove_duplicate_nodes(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
21+
match head {
22+
None => head,
23+
Some(mut head) => {
24+
let mut set = HashSet::new();
25+
set.insert(head.val);
26+
let mut pre = &mut head;
27+
while let Some(cur) = &pre.next {
28+
if set.contains(&cur.val) {
29+
pre.next = pre.next.take().unwrap().next;
30+
} else {
31+
set.insert(cur.val);
32+
pre = pre.next.as_mut().unwrap();
33+
}
34+
}
35+
Some(head)
36+
}
37+
}
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function removeDuplicateNodes(head: ListNode | null): ListNode | null {
14+
if (head == null) {
15+
return head;
16+
}
17+
const set = new Set<number>([head.val]);
18+
let cur = head;
19+
while (cur.next != null) {
20+
if (set.has(cur.next.val)) {
21+
cur.next = cur.next.next;
22+
} else {
23+
set.add(cur.next.val);
24+
cur = cur.next;
25+
}
26+
}
27+
return head;
28+
}

0 commit comments

Comments
 (0)