-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path138_Copy List with Random Pointer.cpp
More file actions
44 lines (41 loc) · 1.21 KB
/
Copy path138_Copy List with Random Pointer.cpp
File metadata and controls
44 lines (41 loc) · 1.21 KB
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
43
44
// A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
// Return a deep copy of the list.
/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
// 每次取node->next->next都要先检验node->next是否可能会null
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
if (!head) return NULL;
RandomListNode *old = head, *copy;
// copy the old node immediately after itself
while (old) {
copy = new RandomListNode(old->label);
copy->next = old->next; old->next = copy;
old = copy->next;
}
// set the random node
old = head;
while (old) {
copy = old->next;
if (old->random) copy->random = old->random->next; // corner case
old = copy->next;
}
// restore the list
RandomListNode *head2 = head->next;
old = head;
while (old) {
copy = old->next;
old->next = copy->next;
if (copy->next) copy->next = copy->next->next; // corner case
old = old->next;
}
return head2;
}
};