LeetCode138 Copy List with Random Pointer (deep replication of linked lists with Random pointers) Java question

Source: Internet
Author: User

LeetCode138 Copy List with Random Pointer (deep replication of linked lists with Random pointers) Java question

Question:

 

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.


Solution:

This is to copy a linked list. This linked list has one more pointer than a common linked list. This pointer can point to any place, be empty, or be any node in the linked list, at noon today, my classmates talked to me about this question. When I asked me how to solve the problem, I thought about the basic ideas: create a new linked list during the first time of the hash table, regardless of the random pointer field, this will be processed when it is retained for the second time.

In the evening, I began to write code for implementation. It may be because I haven't been reading books for a while, and I found it a little unfamiliar. I think it is very important for programmers to implement their own ideas. The most basic requirement is to first implement your ideas and then consider optimization. Xi has said that he has made an empty talk about the country by mistake, and he is actually working on xingbang. Well, I started writing. I found many problems during writing. It was really too long to write.

I used a hash table when I first solved the problem. Now I think I am also confused. However, when I optimized it later, I gradually got two hash tables and one hash table.

The three solutions are actually the same, but there was a lot of redundancy at the beginning.

Solution 1:

 

Public static RandomListNode copyRandomList (RandomListNode head) {// defines the HashMap table
 
  
Hashtable = new HashMap <> (); // saves the HashMap of the linked list node pointed to by random
  
   
Newhashtable = new HashMap <> (); // Save the new linked list node HashMap
   
    
Hashtable2 = new HashMap <> (); // Save the original node // return result RandomListNode newHead = head; if (head = null) // empty linked list return head; if (head. next = null) // {RandomListNode tNode = new RandomListNode (head. label); if (head. random! = Null) tNode. random = tNode; return tNode;} RandomListNode cur = head; // create a hash table and create a new linked list array (int I = 0) other than the empty random pointer over the first time; 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; // record the first node of the new linked list cur = cur. next; I ++;} // process 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); // random pointer pointing to the newhashtable number. 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) {// create two hash tables to store the original node and random pointer to the original node and new node respectively (in this case, deep replication must be new) HashMap
 
  
RandomMap = new HashMap <> (); HashMap
  
   
NewMap = new HashMap <> (); // return result RandomListNode fakeNode = new RandomListNode (-1); // store the node into the hash table RandomListNode cur = head for the first traversal; 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;} // traverse the hash table and add the random pointer Iterator to the linked list to be copied.
   
    
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 result return fakeNode. next ;}
   
  
 

Solution 3:

 

 

Public static RandomListNode copyRandomList3 (RandomListNode head) {HashMap
 
  
NewMap = new HashMap <> (); // For the first traversal, save the node to the 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 );}}
 

 

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.