Skip to content

Commit 55fb4c1

Browse files
authored
feat: add javascript solution to lcof problem: No.35 (doocs#676)
面试题 35. 复杂链表的复制
1 parent 89ad7e8 commit 55fb4c1

File tree

1 file changed

+46
-0
lines changed
  • lcof/面试题35. 复杂链表的复制

1 file changed

+46
-0
lines changed

lcof/面试题35. 复杂链表的复制/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,52 @@ public class Solution {
280280

281281
### **JavaScript**
282282

283+
- 哈希表
284+
285+
```js
286+
/**
287+
* // Definition for a Node.
288+
* function Node(val, next, random) {
289+
* this.val = val;
290+
* this.next = next;
291+
* this.random = random;
292+
* };
293+
*/
294+
295+
/**
296+
* @param {Node} head
297+
* @return {Node}
298+
*/
299+
var copyRandomList = function (head) {
300+
if (head == null) {
301+
return head
302+
}
303+
304+
let cur = head
305+
const map = new Map();
306+
while (cur != null) {
307+
map.set(cur, new Node(cur.val))
308+
cur = cur.next
309+
}
310+
311+
const res = new Node();
312+
let newCur = res
313+
cur = head
314+
while (cur != null) {
315+
const node = map.get(cur)
316+
node.random = map.get(cur.random)
317+
newCur.next = node
318+
newCur = node
319+
cur = cur.next
320+
}
321+
322+
return res.next
323+
};
324+
325+
```
326+
327+
- 原地算法
328+
283329
```js
284330
/**
285331
* // Definition for a Node.

0 commit comments

Comments
 (0)