Leetcode -- Copy list with random pointer (deep copy of a single-chain table referenced by random)

Source: Internet
Author: User
Problem:

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

Return a deep copy of the list.

 

The node is 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 ;}
*};
*/

Analysis:

Shows the structure of the original linked list:

Note that the random pointer can point to the subsequent node or the previous node.

 

Here we provide two solutions:

 

Method 1: Use a 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 with a random value of null. At the same time, store the address information of the original and new nodes in the hash table. The original Node Address is key, and the new node address is value, that is, Map <original Node Address, new node address>

The second traversal of the original linked list. 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 based on the random value, assign a value to the new node random.

 

Method 2: first change the structure of the original linked list, at the recovery time O (2n), Space O (1)

This method requires three traversal times,

For the first time, the node for creating the new linked list, the random is null, And the next of the original linked list node points to the corresponding new node, the next of the new node points to the next node of the original linked list ,:

 

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, the structure of the original and new linked lists is restored.

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;    }}

Leetcode -- Copy list with random pointer (deep copy of a single-chain table referenced by random)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.