Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit55b8615

Browse files
committed
Merge pull requestneetcode-gh#178 from HangCcZ/main
Added 138-Copy-List-with-Random-Pointer.js
2 parents38e0811 +d0bf3bb commit55b8615

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
*@param {Node} head
3+
*@return {Node}
4+
*/
5+
varcopyRandomList=function(head){
6+
letmap=newMap();
7+
letptr=head;
8+
9+
while(ptr){
10+
// map old - new
11+
map.set(ptr,newNode(ptr.val,null,null));
12+
ptr=ptr.next;
13+
}
14+
15+
ptr=head;
16+
while(ptr){
17+
map.get(ptr).next=map.get(ptr.next)||null;
18+
map.get(ptr).random=map.get(ptr.random)||null;
19+
ptr=ptr.next;
20+
}
21+
returnmap.get(head);
22+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp