Skip to content

Commit 737ba0c

Browse files
committed
Merge branch 'HangCcZ-main' into main
2 parents e527e8e + 607a679 commit 737ba0c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {Node} head
3+
* @return {Node}
4+
*/
5+
var copyRandomList = function (head) {
6+
let map = new Map();
7+
let ptr = head;
8+
9+
while (ptr) {
10+
// map old - new
11+
map.set(ptr, new Node(ptr.val, null, null));
12+
ptr = ptr.next;
13+
}
14+
15+
for (const [ oldptr, newptr ] of map) {
16+
newptr.next = oldptr.next && map.get(oldptr.next);
17+
newptr.random = oldptr.random && map.get(oldptr.random);
18+
}
19+
return map.get(head);
20+
};

0 commit comments

Comments
 (0)