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
public void copyNodes(RandomListNode pHead){
        RandomListNode walkNode=pHead;
         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
    public void initRandom(RandomListNode pHead){
         RandomListNode walkNode=pHead;
         RandomListNode cwalkNode=pHead;
         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
     public RandomListNode split2list(RandomListNode pHead){
         RandomListNode cpHead=pHead.next;
         RandomListNode walkNode=pHead;
         RandomListNode cwalkNode=cpHead;
         while(walkNode!=null){
             walkNode.next=cwalkNode.next;
             walkNode=walkNode.next;
             if(walkNode==null){
                 cwalkNode.next=null;
             }
             else{
                 cwalkNode.next=walkNode.next;
                 cwalkNode=cwalkNode.next; 
             }
         }
         return cpHead;
     }

4. Total calling functions

public RandomListNode Clone(RandomListNode pHead)
{
         if(pHead==null){
             return null;
         }
         copyNodes(pHead);
         initRandom(pHead);
         return split2list(pHead);
 }

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
12     public void copyNodes(RandomListNode pHead){
13         RandomListNode walkNode=pHead;
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
22      public void initRandom(RandomListNode pHead){
23          RandomListNode walkNode=pHead;
24          RandomListNode cwalkNode=pHead;
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
34      public RandomListNode split2list(RandomListNode pHead){
35          RandomListNode cpHead=pHead.next;
36          RandomListNode walkNode=pHead;
37          RandomListNode cwalkNode=cpHead;
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          }
49          return cpHead;
50      }
51      public RandomListNode Clone(RandomListNode pHead)
52      {
53          if(pHead==null){
54              return null;
55          }
56          copyNodes(pHead);
57          initRandom(pHead);
58          return split2list(pHead);
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);
70             head.next = node2;
71             node2.next = node3;
72             node3.next = node4;
73             node4.next = node5;
74             head.random = node3;
75             node2.random = node5;
76             node4.random = node2;
77             copyList s = new copyList();
78             RandomListNode copyList=s.Clone(head);
79             System.out.print(copyList.label + " ");
80 //            while (copyList != null)
81 //            {
82 //                System.out.print(copyList.label + " ")
83 //                copyList = copyList.next;
84 //            }
85      }
86 }

Posted on Sat, 16 May 2020 19:54:17 -0400 by mark_php