-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-list-with-random-pointer.cpp
More file actions
31 lines (31 loc) · 1.15 KB
/
copy-list-with-random-pointer.cpp
File metadata and controls
31 lines (31 loc) · 1.15 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
// 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) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *copy,*p;
if (!head) return NULL;
for(p=head;p;p=p->next){
copy = new RandomListNode(p->label);
copy->next = p->next; // insert new at old next
p = p->next = copy;
}
for(p=head;p;p=copy->next){
copy = p->next; // copy random point
copy->random = (p->random?p->random->next:NULL);
}
for(p=head,head=copy=p->next;p;){
p = p->next = copy->next; // split list
copy = copy->next = (p?p->next:NULL);
}
return head;
}
};