-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy_List.cpp
More file actions
43 lines (42 loc) · 1.1 KB
/
Copy_List.cpp
File metadata and controls
43 lines (42 loc) · 1.1 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
/**
* Definition for singly-linked list.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
RandomListNode* Solution::copyRandomList(RandomListNode* A) {
if(!A)
return A;
RandomListNode* tmp1 = A;
while(tmp1){
RandomListNode* copy = new RandomListNode(tmp1->label);
copy->next = tmp1->next; // new.next = old.next
tmp1->next = copy;// old.next = new
tmp1 = copy->next;//old = new.next(i.e. previous old.next)
}
tmp1 = A;
RandomListNode* tmp2 = A->next;
while(tmp1){
if(tmp1->random)
tmp2->random = tmp1->random->next;
else
tmp2->random = NULL;
tmp1 = tmp2->next;
if(tmp1)
tmp2 = tmp1->next;
}
RandomListNode* head = A->next;
tmp1 = A;
tmp2 = A->next;
while(tmp1){
tmp1->next = tmp2->next;
tmp1 = tmp1->next;
if(tmp1){
tmp2->next = tmp1->next;
tmp2 = tmp2->next;
}
}
return head;
}