Skip to content

Commit 357d212

Browse files
committed
Time: 46 ms (92.93%), Space: 51 MB (32.54%) - LeetHub
1 parent 89349b1 commit 357d212

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+
* // Definition for a _Node.
3+
* function _Node(val, next, random) {
4+
* this.val = val;
5+
* this.next = next;
6+
* this.random = random;
7+
* };
8+
*/
9+
10+
/**
11+
* @param {_Node} head
12+
* @return {_Node}
13+
*/
14+
var copyRandomList = function (head) {
15+
const map = new Map();
16+
let current = head;
17+
while (current) {
18+
map.set(current, new _Node(current.val));
19+
current = current.next
20+
}
21+
current = head;
22+
while (current) {
23+
let copiedNode = map.get(current);
24+
copiedNode.next = map.get(current.next) || null;
25+
copiedNode.random = map.get(current.random) || null;
26+
current = current.next;
27+
}
28+
29+
return map.get(head);
30+
};

0 commit comments

Comments
 (0)