-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path138.cpp
30 lines (30 loc) · 874 Bytes
/
138.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
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head)
return nullptr;
auto node = head;
while (node) {
auto next = new RandomListNode(node->label);
next->next = node->next;
node->next = next;
node = node->next->next;
}
node = head;
auto next = node->next, ret = next;
while (node) {
if (node->random)
next->random = node->random->next;
node = node->next->next;
next = node ? node->next : nullptr;
}
node = head;
while (node) {
auto temp = node->next;
node->next = node->next->next;
temp->next = node->next ? node->next->next : nullptr;
node = node->next;
}
return ret;
}
};