-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlt60.cpp
42 lines (38 loc) · 848 Bytes
/
lt60.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node* copyRandomList(Node* head) {
if(head==NULL){
return nullptr;
}
unordered_map<Node*,Node*> bind;
Node* current;
current= head;
while(current!=NULL){
Node* newNode = new Node(current->val);
bind[current]= newNode;
current=current->next;
}
current=head;
while(current!=NULL){
bind[current]->next= bind[current->next];
bind[current]->random= bind[current->random];
current=current->next;
}
current= head;
return bind[current];
}
};