What is a complex linked list?
In a complex list, each node has a _next pointer pointing to the next node, and a _random to any node in the list or null. The nodes are defined as follows:
Template <class t>struct complexnode{public:complexnode (const t&d): _data (d), _next (null), _random (null) {} Public:t _data;//data complexnode* _next;//refers to the next node complexnode* _random;//points to a random node or null};
is a complex linked list with 5 nodes. The solid arrow is the _next pointer, and the dashed line is the _random pointer.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7F/11/wKiom1cR1DbzGXdpAAAPM-P8yl0887.png "title=" 3.PNG " alt= "Wkiom1cr1dbzgxdpaaapm-p8yl0887.png"/>
When you see this problem, you first think of splitting the replication process into two parts: the first step is to copy each node on the original list and connect it with _next, and the second step is to set the _random pointer for each node. Assuming that the _random of node n in the original list points to node s, the position of the S node may be in front of N, or it may be behind N. So to locate the position of s you need to find it from the beginning. If the node s is found in n steps from the beginning of the list's head node along _next, then the _random of Node n ' In the replication list is also n steps away from the head node of the replication list. If there are N nodes, finding _random for each node requires finding it from the beginning. Therefore, the time complexity of this method is O (n^2).
Because the time of the above method is mainly spent on the _random of the lookup node. Can be optimized from this aspect.
The first step is to create the corresponding n ' from each node n of the original linked list and connect it after N. () 650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/11/wKiom1cR2VHzqIUZAAAb4SUQM_g474.png "title=" 4. PNG "alt=" Wkiom1cr2vhzqiuzaaab4suqm_g474.png "/>
Step two: Set the _random of the node to be copied out. Assuming that the _random of node n on the original list points to the node S, it can be seen from the graph that n ' _random is the node of the _next point of N's _random. (shown on the red Line) 650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/11/wKiom1cR20XyZJOrAAAkJy_gOVI546.png "title=" 5.PNG "alt=" wkiom1cr20xyzjoraaakjy_govi546.png "/> Step three: Split the list. Connecting the nodes of odd digits with _next is the original linked list, and the nodes on the even digits are connected with _next to be the replicated nodes.
In this view, the time complexity is O (n).
Next, implement the second way of thinking.
First step: Copy Nodes
Template <class t>void copynode (complexnode<t>* phead)//Copy node {complexnode<t>* cur = phead;while (cur) {complexnode<t>* Pclone = new complexnode<t> (cur->_data);//Create a node Pclone->_next = cur->_next; Pclone->_random = Null;cur->_next = Pclone;cur = Pclone->_next;}}
Step two: Set the _random of the copied node
Template <class t>void randomnode (complexnode<t>* phead)//find _random{complexnode<t>* cur = pHead; complexnode<t>* Pclone = Phead->_next;while (pclone->_next) {if (cur->_random! = NULL) {pClone->_ Random = cur->_random->_next;//The _random of the copy node is the _random pointer of the original node _next}cur = Pclone->_next;pclone = Cur->_next ;}}
Step three: Split the list of links
Template <class t>complexnode<t>* connectnode (complexnode<t>* phead)//split list {complexnode<t>* cur = phead;//odd digit complexnode<t>* Pclonenode = phead->_next;//even digit complexnode<t>* PCloneHead = pHead-> _next;while (pclonenode->_next) {cur->_next = pclonenode->_next;//original node link cur = cur->_next;pclonenode-> _next = cur->_next;//The link of the node after the copy pclonenode = Pclonenode->_next;} return pclonehead;}
To create a replication link list:
Template <class t>complexnode<t>* creatnode (complexnode<t>* phead)//construct complex linked list {complexnode<t>* Node1 = new complexnode<t> (1); complexnode<t>* Node2 = new complexnode<t> (2); complexnode<t>* Node3 = new complexnode<t> (3); complexnode<t>* Node4 = new complexnode<t> (4); complexnode<t>* NODE5 = new complexnode<t> (5);p head = Node1; Node1->_next = Node2; Node2->_next = Node3; Node3->_next = Node4; Node4->_next = NODE5; Node1->_random = Node3; Node2->_random = NODE5; Node4->_random = Node2;return Phead;}
Print linked list:
Template <class t>void printnode (complexnode<t>* phead)//print {complexnode<t>* cur = phead;while (cur) { cout<<cur->_data<< ""; if (cur->_random)//If _random is not empty {cout<<cur->_random->_data;} cout<< "--"; cur = cur->_next;} Cout<<endl;}
Main function Implementation:
int main () {complexnode<int>* head = NULL; complexnode<int>* ret = Creatnode (head);//create complex linked list cout<< "original linked list:" <<endl; Printnode (ret); Copynode (ret);//Replication Node Randomnode (ret);//_randomcomplexnode<int>* TMP = Connectnode (ret) of the node after the replication,//Split node cout << "linked list after copying:" <<endl; Printnode (TMP); return 0;}
Test results:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7F/0F/wKioL1cR31jhFsnZAAAQSHmGokM169.png "title=" 1.PNG " alt= "Wkiol1cr31jhfsnzaaaqshmgokm169.png"/>
This article from "Together to see the Stars" blog, reproduced please contact the author!
Replication of complex linked lists