Skip to content

Commit 388c522

Browse files
authored
feat: add javascript solution to lcci problem: No.02.01.Remove Duplicate Node (doocs#366)
1 parent 6db9f51 commit 388c522

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

lcci/02.01.Remove Duplicate Node/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,38 @@ class Solution {
102102
}
103103
```
104104

105+
### **JavaScript**
106+
107+
```javascript
108+
/**
109+
* Definition for singly-linked list.
110+
* function ListNode(val) {
111+
* this.val = val;
112+
* this.next = null;
113+
* }
114+
*/
115+
/**
116+
* @param {ListNode} head
117+
* @return {ListNode}
118+
*/
119+
var removeDuplicateNodes = function(head) {
120+
if (head == null || head.next == null) return head;
121+
const cache = new Set([]);
122+
cache.add(head.val);
123+
let cur = head, fast = head.next;
124+
while (fast !== null) {
125+
if (!cache.has(fast.val)) {
126+
cur.next = fast;
127+
cur = cur.next;
128+
cache.add(fast.val);
129+
}
130+
fast = fast.next;
131+
}
132+
cur.next = null;
133+
return head;
134+
};
135+
```
136+
105137
### **...**
106138

107139
```

lcci/02.01.Remove Duplicate Node/README_EN.md

+32
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,38 @@ class Solution {
103103
}
104104
```
105105

106+
### **JavaScript**
107+
108+
```javascript
109+
/**
110+
* Definition for singly-linked list.
111+
* function ListNode(val) {
112+
* this.val = val;
113+
* this.next = null;
114+
* }
115+
*/
116+
/**
117+
* @param {ListNode} head
118+
* @return {ListNode}
119+
*/
120+
var removeDuplicateNodes = function(head) {
121+
if (head == null || head.next == null) return head;
122+
const cache = new Set([]);
123+
cache.add(head.val);
124+
let cur = head, fast = head.next;
125+
while (fast !== null) {
126+
if (!cache.has(fast.val)) {
127+
cur.next = fast;
128+
cur = cur.next;
129+
cache.add(fast.val);
130+
}
131+
fast = fast.next;
132+
}
133+
cur.next = null;
134+
return head;
135+
};
136+
```
137+
106138
### **...**
107139

108140
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var removeDuplicateNodes = function(head) {
13+
if (head == null || head.next == null) return head;
14+
const cache = new Set([]);
15+
cache.add(head.val);
16+
let cur = head, fast = head.next;
17+
while (fast !== null) {
18+
if (!cache.has(fast.val)) {
19+
cur.next = fast;
20+
cur = cur.next;
21+
cache.add(fast.val);
22+
}
23+
fast = fast.next;
24+
}
25+
cur.next = null;
26+
return head;
27+
};

0 commit comments

Comments
 (0)