Problem:
Copy pointer random list containing node
[Title] A special class list node is described as follows:
public class the Node {
public int value; public Next the Node; public
the Node RAND;
public the Node (int Data) = Data {this.value ;}
}
node class value is the node value, pointer meaning next pointer and a normal single-linked list next
one sample, all point to the next node, RAND pointers node class new pointer, this means
the needle may point to a linked list any node, it may point to null. Given a the
head node of the single chain acyclic head node type Node consist of, a function to achieve complete
copy of all of the linked list structure, and the head node returns the new list of replication. Advanced:
no additional data structures, only a limited number of variables, and the time complexity is (N) O
completion function to be implemented within the original problem.
Solution :
a:
used to store the original list hash_map
key = original list, value = new linked list node
and list the structure of the original list of new structural configuration of
two:
the original list array place two copies:
head 2. 3. 1 4 5 6 NULL
copy to: head head '1 1' 2 2 '3 3' 4 4 '5 5' 6 '6' NULL
node then takes the band copy 'on the line
1 #pragma once 2 #include <iostream> 3 #include <hash_map> 4 5 using namespace std; 6 7 struct Node 8 { 9 int val; 10 Node *rand; 11 Node *next; 12 Node(int a = -1) :val(a), rand(NULL), next(NULL) {} 13 }; 14 15 16 Node* CopeListDeep(Node* head) 17 { 18 hash_map <* the Node, the Node *> Map; . 19 the Node * P = head; 20 is the while (P) // structure of the new linked list 21 is { 22 is Map [P] = new new the Node (- . 1 ); 23 is P = p-> Next ; 24 } 25 26 is P = head; 27 the while (P) // reconstructed new list structure 28 { 29 Map [P] -> = p-Val> Val; 30 Map [P] -> Next Map = [P -> Next]; 31 is map[p]->rand = map[p->rand]; 32 p = p->next; 33 } 34 return map[head]; 35 } 36 37 Node* CopeListDeep2(Node* head) 38 { 39 Node* cur = head; 40 Node* next = NULL; 41 while (cur)//将原链表复制两份 42 { 43 next = cur->next; 44 cur->next = new Node(cur->val); 45 cur> next-> Next = Next; // here the two duplicated code 46 is CUR = Next; 47 } 48 49 CUR = head; 50 the Node copyHead * = NULL; // To distinguish the original strand 51 // first copy rand node 52 is the while (CUR) // the structure of the new node reconstructs 53 is { 54 is Next cur => next-> Next; // original traversal list 55 copyHead = cur> Next; 56 is copyHead-> = RAND (? (cur> RAND) == NULL NULL: (cur> RAND) -> the Next); 57 cur = next; 58 } 59 //copyHead已经是链表的末尾NULL 60 cur = head; 61 Node* res = head->next; 62 while (cur) 63 { 64 next = cur->next->next; 65 copyHead = cur->next; 66 cur->next = next; 67 copyHead->next = (next == NULL) ? NULL : next->next; 68 cur = next; 69 } 70 71 //Why not a one-time reduction and copy the original list list reconstructed? 72 // because the front once the original list half of the reduction, the last half once the first half of the rand point, the original list is to find the points 73 // node, but since the front half of the original list has been restored, so copying the list rand not find their own node rand pointed according to the position of the original list of 74 // it was first rand copy it. 75 return RES; 76 77 } 78 79 80 void the Test () 81 { 82 the Node head * = new new the Node (- . 1 ); 83 head-> Next = new new the Node ( . 1 ); 84 head-> next-> Next = new new the Node ( 2 ); 85 head->next->next->next = new Node(3); 86 head->next->next->next->next = new Node(4); 87 head->next->next->next->next->next = new Node(5); 88 head->next->next->next->next->next->next = new Node(6); 89 head->next->next->next->next->next->next->next = NULL; 90 91 92 head->rand = NULL; 93 head->next->rand = head->next->next->next->next->next->next; 94 head->next->next->rand = head->next->next->next->next->next->next; 95 head->next->next->next->rand = head->next->next->next->next->next; 96 head->next->next->next->next->rand = head->next->next->next; 97 head->next->next->next->next->next->rand = NULL; 98 head->next->next->next->next->next->next->rand = head->next->next->next->next; 99 100 101 cout << "打印原链表:" << endl; 102 Node*p = head->next; 103 while (p) 104 { 105 cout << "next: " << p->val << " rand: " << ((p->rand) ? p->rand->val : -1) << endl; 106 p = p->next; 107 } 108 109 cout << endl << "打印新链表:" << endl; 110 p = CopeListDeep2(head)->next; 111 while (p) 112 { 113 cout << "next: " << p->val << " rand: " << ((p->rand) ? p->rand->val : -1) << endl; 114 p = p->next; 115 } 116 117 118 }
Reproduced in: https: //www.cnblogs.com/zzw1024/p/10993026.html