LeetCode138 copy List with random Pointer (deep copy linked list with stochastic pointers) Java

Source: Internet
Author: User

Topic:

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.


Solving:

This problem is to copy a linked list, this list is more than the normal list of a pointer, this pointer can point to any place, can be empty or any node in the list, today at noon, my classmates and I said this question, then asked me the problem-solving ideas, I thought about the basic idea: Create a new linked list with a hash table the first time you traverse it. This time, regardless of the random pointer field, this is treated with a second pass.

In the evening I began to write code implementation, may be because there are no how to brush all the questions are reading, found a little rusty. I think the programmer to realize their own ideas is a very important ability, the most basic requirement is to implement your ideas, and then to consider the optimization. Xi greatly said, empty talk endangers, hard work hing bang. Well I started to write, when I found a lot of problems, it really is too long not written.

The first time I used a hash table, I think it is also drunk, but after the optimization of the time gradually 2 hash table 1 hash table.

Three methods of solution are actually the same, but initially there are a lot of redundant places.

Solution 1:

 public static Randomlistnode Copyrandomlist (Randomlistnode head) {//define hash table Hashmap<integer, randomlistnode> Hasht Able=new hashmap<> ();//save random point to the linked list node Hashmap<integer, randomlistnode> newhashtable=new HashMap<> ();//Save the new list node hashmap<randomlistnode,integer> hashtable2=new hashmap<> ();//Save the original node//return result Randomlistnode  Newhead=head; if (head==null)//Empty list return head; if (head.next==null)//One node {randomlistnode tnode=new randomlistnode (Head.label); if (head.random!=null) tNode.random =tnode; return tnode; } Randomlistnode Cur=head; The first traversal builds a hash table and creates an array of new linked lists except the random pointer is empty int i=0; while (Cur!=null) {hashtable.put (I, cur.random); Hashtable2.put (cur,i); Randomlistnode tnode=new Randomlistnode (Cur.label);  Newhashtable.put (i, tnode); if (i==0) newhead=tnode;//records the first node of a new list cur=cur.next; i++; }//Iterate over the handling of the random pointer int j=0; Cur=head; while (Cur!=null) {if (j>0) {newhashtable.get (j-1). Next=newhashtable.get (j);} if (Hashtable2.get (Hashtable.get (j) )!=null) {int key=hAshtable2.get (Hashtable.get (j));//The random pointer points to the number of Newhashtable.get (j). Random=newhashtable.get (key); } else {Newhashtable.get (j). Random=null;} cur=cur.next; j + +;              } return newhead; }

Solution 2:

 public static Randomlistnode CopyRandomList2 (Randomlistnode head) {//New two hash tables are stored in the original node and the random pointer original node and the new node (this requires that deep copy must be new in order to To) Hashmap<randomlistnode, randomlistnode> randommap=new hashmap<> ();  Hashmap<randomlistnode, randomlistnode> newmap=new hashmap<> ();  Return results Randomlistnode fakenode=new Randomlistnode (-1); For the first traversal, the node is stored in a hash table Randomlistnode cur=head; Randomlistnode Pre=fakenode; while (Cur!=null) {randommap.put (cur, cur.random); Randomlistnode newnode=new Randomlistnode (Cur.label);  Newmap.put (cur, newNode); Cur=cur.next; Pre.next=newnode;   Pre=pre.next; }//The hash table is traversed as a linked list that needs to be copied plus a random pointer iterator<randomlistnode> iterator=randommap.keyset (). Iterator (); while (Iterator.hasnext ()) {Randomlistnode node= (randomlistnode) iterator.next (); Randomlistnode copynode=newmap.get (node); Randomlistnode randnode=randommap.get (node); if (randnode!=null) copynode.random=newmap.get (Randnode);  else {copynode.random=null;}     }//Return the result return fakenode.next; }

Solution 3:

public static Randomlistnode CopyRandomList3 (Randomlistnode head) {  Hashmap<randomlistnode, Randomlistnode > Newmap=new hashmap<> (); For the first traversal,  the node is stored in a hash table Randomlistnode cur=head; while (cur!=null) {Randomlistnode newnode=new Randomlistnode ( Cur.label); Newmap.put (cur, newNode); Cur=cur.next; }  Cur=head, while (cur!=null) {Randomlistnode node=newmap.get (cur); Node.next=newmap.get (cur.next); node.random= Newmap.get (Cur.random); Cur=cur.next; } return Newmap.get (head);}   }


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

LeetCode138 copy List with random Pointer (deep copy linked list with stochastic pointers) Java

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.