Copy List with Random Pointer

Source: Internet
Author: User

https://leetcode.com/problems/copy-list-with-random-pointer/

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

Return a deep copy of the list.

Problem Solving Ideas:

What is deep copy, what is shallow copy? In Java, shallow copy means that B is copied to a, but B and a point to the same object. Deep copy means that B and a point to different objects, but the two objects are exactly the same.

Refer to Http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java for details

Then the difficulty of this problem is to have a random pointer. The current node and next are easy to copy, traverse through, and continue to the new node according to the value of the original linked list, and then point to the next node. The question is, how do I find the random node of the current node the second time?

Here is hashmap this data structure, the first time traversal, the establishment of a map,key is the original linked list node, value is the node of the current list. We know that the map is actually stored in the corresponding address. In this way, the second time to find the new list of nodes of the current node of the random, as long as the map to find the original linked list of the value of the node can be.

The code is as follows:

/*** Definition for singly-linked list with a random pointer. * Class Randomlistnode {* int label; * Randomlist Node Next, Random; * Randomlistnode (int x) {This.label = x;}}; */ Public classSolution { PublicRandomlistnode copyrandomlist (Randomlistnode head) {if(Head = =NULL) {            returnHead; } Map<randomlistnode, randomlistnode> map =NewHashmap<randomlistnode, randomlistnode>(); Randomlistnode Copyhead=NewRandomlistnode (Head.label); Randomlistnode Head1=Head; Randomlistnode Returnhead=Copyhead;        Map.put (Head1, Copyhead);  while(Head1.next! =NULL) {Copyhead.next=NewRandomlistnode (Head1.next.label);            Map.put (Head1.next, Copyhead.next); Copyhead=Copyhead.next; Head1=Head1.next; } copyhead=Returnhead; Head1=Head;  while(Copyhead! =NULL) {Copyhead.random=Map.get (head1.random); Copyhead=Copyhead.next; Head1=Head1.next; }        returnReturnhead; }}

The above solution should be relatively easy to understand. Google to netizens there is another good solution, you can only spend extra O (1) space, using the same O (n) time.

Do this:

1. After each node in the original list, insert an identical copy node. The next order of copy will naturally come out. Like 1-1 '-2-2 '-3-3 '

2. Point the random of each replicated node to the next node of the random of its previous node

3. Extract all replicated nodes in the order of next, forming a new linked list and returning.

The code is as follows

/*** Definition for singly-linked list with a random pointer. * Class Randomlistnode {* int label; * Randomlist Node Next, Random; * Randomlistnode (int x) {This.label = x;}}; */ Public classSolution { PublicRandomlistnode copyrandomlist (Randomlistnode head) {if(Head = =NULL) {            returnHead; } Randomlistnode Returnhead=Head; //insert a new node after each node in the original list         while(Head! =NULL) {Randomlistnode NewNode=NewRandomlistnode (Head.label); Newnode.next=Head.next; Head.next=NewNode; Head=Head.next.next; }                //The random of each replicated node points to its previous node of the random nextHead =Returnhead;  while(Head! =NULL) {            if(Head.random! =NULL) {Head.next.random=Head.random.next; } head=Head.next.next; }                //extract each replicated node and returnHead =Returnhead; Returnhead=Returnhead.next;  while(Head! =NULL) {Randomlistnode NewNode=Head.next; Head.next=Head.next.next; //this boundary condition must be noted, otherwise the original list only one node will be out of bounds            if(Newnode.next! =NULL) {Newnode.next=NewNode.next.next; } head=Head.next; }        returnReturnhead; }}

As you can see, the idea above is also to use some method to find the random node of each node in the newly copied list, the method still depends on the original linked list. But the train of thought is very ingenious, worth understanding.

Copy List with Random Pointer

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.