Skip to content

Commit 81822d4

Browse files
committed
Time: 10 ms (77.35%), Space: 11.4 MB (27.28%) - LeetHub
1 parent de0766a commit 81822d4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
Node* next;
7+
Node* random;
8+
9+
Node(int _val) {
10+
val = _val;
11+
next = NULL;
12+
random = NULL;
13+
}
14+
};
15+
*/
16+
17+
class Solution {
18+
public:
19+
20+
Node* copyRandomList(Node* head) {
21+
map<Node*, Node*> m;
22+
int i=0;
23+
Node* ptr = head;
24+
while (ptr) {
25+
m[ptr] =new Node(ptr->val);
26+
ptr = ptr->next;
27+
}
28+
ptr = head;
29+
while (ptr) {
30+
m[ptr]->next = m[ptr->next];
31+
m[ptr]->random = m[ptr->random];
32+
ptr = ptr->next;
33+
}
34+
return m[head];
35+
}
36+
};

0 commit comments

Comments
 (0)