10000 19/09/01 · freezeYe/leetcode-js@68541e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68541e4

Browse files
author
zhangbo
committed
19/09/01
1 parent 5317508 commit 68541e4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
3+
* Return a deep copy of the list.
4+
*/
5+
6+
/**
7+
* // Definition for a Node.
8+
* function Node(val,next,random) {
9+
* this.val = val;
10+
* this.next = next;
11+
* this.random = random;
12+
* };
13+
*/
14+
/**
15+
* @param {Node} head
16+
* @return {Node}
17+
*/
18+
var copyRandomList = function (head) {
19+
const m = new Map()
20+
function helper(node, m) {
21+
if (!node) return null
22+
if (m.has(node)) return m.get(node)
23+
const n = new Node(node.val, null, null)
24+
m.set(node, n)
25+
n.next = helper(node.next, m)
26+
n.random = helper(node.random, m)
27+
return n
28+
}
29+
return helper(head, m)
30+
};

0 commit comments

Comments
 (0)
0