Random pointer singly linked list deep copy (LeetCode - Copy List with Random Pointer)

question:

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

 

Nodes are defined as follows:

/** 
* Definition for singly-linked list with a random pointer. 
* class RandomListNode { 
*     int label; 
*     RandomListNode next, random; 
*     RandomListNode(int x) { this.label = x; } 
* }; 
*/

analyze:

The structure of the original linked list is shown in the following figure:

image

Note that the random pointer can point to the back node, or it can point to the front node.

 

Here are two solutions:

 

Method 1: Use hash table to store node information, time O(2n), space O(n)

Traverse the original linked list for the first time, and construct a new linked list whose random is null. At the same time, the address information of the original node and the new node is stored in the hash table. The address of the original node is the key, and the address of the new node is the value, that is, map<original node address, new node address>

Traverse the original linked list for the second time. For nodes whose random is not empty, you can find the address of the new node corresponding to the node pointed to by random in the hash table according to the value of random, and then assign a value to the new node random. .

 

Method 2: Change the structure of the original linked list first, then restore, time O(2n), space O(1)

This method requires 3 traversals,

For the first time, the node of the new linked list is constructed, random is null, and the next node of the original linked list node points to the corresponding new node, and the next node of the new node points to the next node of the original linked list, as shown in the figure:

 

image

The second time, assign a value to the random of the new node,

p = head;
        while(p!=null){
            if(p.random != null){
                p.next.random = p.random.next;
            }
            p = p.next.next;
        }

The third time, restore the linked list structure of the original linked list and the new linked list.

head2 = head.next;
        p = head;
        while(p!=null){
            p2 = p.next;
            p.next = p2.next;
            if (p2.next != null) p2.next = p2.next.next;
            p = p.next;
        }

 Complete code for method 2:

public class Solution {
    public RandomListNode copyRandomList(RandomListNode head) {
        if(head == null)return null;
        RandomListNode head2,p2,p = head;
        while(p!=null){
            RandomListNode n = new RandomListNode(p.label);
            n.next = p.next;
            p.next = n;
            p = n.next;
        }
        p = head;
        while(p!=null){
            if(p.random != null){
                p.next.random = p.random.next;
            }
            p = p.next.next;
        }
        
        head2 = head.next;
        p = head;
        while(p!=null){
            p2 = p.next;
            p.next = p2.next;
            if (p2.next != null) p2.next = p2.next.next;
            p = p.next;
        }
        return head2;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529769&siteId=291194637