# Replication of Complex Chain List--Cow offer

Title Description:

Enter a complex chain table (each node has a node value and two pointers, one pointing to the next node and another special pointer pointing to any node) and return the head of the replicated complex chain table.(Note that do not return node references in parameters in the output, otherwise the adjudicator will directly return null)

Data structure:

public class RandomListNode {
int label; data domain
RandomListNode next = null;) Point to the next node
RandomListNode random = null;) Point to any node

RandomListNode(int label) {
this.label = label;
}
}

Solving ideas:

1. Traverse the list of chains, copy each node in the list, and insert the copied node behind it.For example, the original chain table is A->B->C, after traversal, the chain table becomes A->A'->B->B'->C->C', where A', B', C'is the replication node of nodes A, B, C.

In the picture, the blue arrow is the next pointer:

After copying the node:

2. Assigning values to random pointers of replicated nodes

If the random pointer of the origin node points to node B, then the random pointer of the replicated node points to the replicated node B'.

The black arrow in the picture is a random pointer:

When the random pointer of a replicated node is assigned:

3. Split the original and the replicated nodes of the chain table into two chains, so that the original nodes form a chain table and the replicated nodes form a chain table.

Code implementation:

1. Duplicate Nodes

`````` //1.join copy node
while(walkNode!=null){
RandomListNode cloneNode=new RandomListNode(walkNode.label);
cloneNode.next=walkNode.next;
walkNode.next=cloneNode;
walkNode=cloneNode.next;
}
}``````

2. Assigning a value to the random pointer field of a replicated node

``````//2.For New copy Nodal random Domain Specified Value
while(walkNode!=null){
cwalkNode=walkNode.next;
if(walkNode.random!=null){
cwalkNode.random=walkNode.random.next;
}
walkNode=cwalkNode.next;
}
}``````

3. Divide nodes and replication nodes into two chains

``````//3.Will list and its copy Versions are divided into two chained lists
while(walkNode!=null){
walkNode.next=cwalkNode.next;
walkNode=walkNode.next;
if(walkNode==null){
cwalkNode.next=null;
}
else{
cwalkNode.next=walkNode.next;
cwalkNode=cwalkNode.next;
}
}
}``````

4. Total calling functions

``````public RandomListNode Clone(RandomListNode pHead)
{
return null;
}
}``````

All Code

`````` 1 class RandomListNode {
2     int label;
3     RandomListNode next = null;
4     RandomListNode random = null;
5     RandomListNode(int label) {
6         this.label = label;
7     }
8 }
9
10 public class copyList {
11      //1.join copy node
14          while(walkNode!=null){
15              RandomListNode cloneNode=new RandomListNode(walkNode.label);
16              cloneNode.next=walkNode.next;
17              walkNode.next=cloneNode;
18              walkNode=cloneNode.next;
19          }
20      }
21     //2.For New copy Nodal random Domain Specified Value
25          while(walkNode!=null){
26              cwalkNode=walkNode.next;
27              if(walkNode.random!=null){
28                  cwalkNode.random=walkNode.random.next;
29              }
30              walkNode=cwalkNode.next;
31          }
32      }
33     //3.Will list and its copy Versions are divided into two chained lists
38          while(walkNode!=null){
39              walkNode.next=cwalkNode.next;
40              walkNode=walkNode.next;
41              if(walkNode==null){
42                  cwalkNode.next=null;
43              }
44              else{
45                  cwalkNode.next=walkNode.next;
46                  cwalkNode=cwalkNode.next;
47              }
48          }
50      }
52      {
54              return null;
55          }
59      }
60      public static void main(String[]args){
61             RandomListNode head = new RandomListNode(1);
63             RandomListNode node2 = new RandomListNode(2);
65             RandomListNode node3 = new RandomListNode(3);
67             RandomListNode node4 = new RandomListNode(4);
69             RandomListNode node5 = new RandomListNode(5);
71             node2.next = node3;
72             node3.next = node4;
73             node4.next = node5;
75             node2.random = node5;
76             node4.random = node2;
77             copyList s = new copyList();