"Leetcode" 138. Copy List with Random Pointer

Source: Internet
Author: User

Title:

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.

Tips:

This problem has two methods, one is to follow the original list of the next order to create the node, and handle the new list of the next pointer, while the original node and the new node corresponding relationship to a hash_map, and then the second cycle of the random pointer processing good. The time complexity of this method is O (n), and the spatial complexity is also O (n).

The second method is to insert a new node after each node of the original list, so that the corresponding relationship between the original node and the new node is clear, so it does not need to be saved with Hash_map, but requires a third loop to split the entire list into two. The time complexity of this method is O (n), and the spatial complexity is O (1).

However, the use of Hash_map method has other advantages, if you add a judgment in the loop, you can detect whether there is a loop in the list, and the second method does not, will fall into the dead loop.

Code:

Ways to use Hash_map:

/** Definition for singly-linked list with a random pointer. * struct RANDOMLISTNODE {* int label; * Randomli Stnode *next, *random; * Randomlistnode (int x): label (x), Next (null), random (null) {} *}; */classSolution { Public: Randomlistnode*copyrandomlist (Randomlistnode *head) {        if(!head)returnNULL; Unordered_map<randomlistnode*, randomlistnode*>MP; //Create a new linked list headerRandomlistnode *new_head =NewRandomlistnode (head->label); //Node1 is responsible for pointing to the original linked list, Node2 is responsible for pointing to the new linked listRandomlistnode *node1 = head, *node2 =New_head; /** * According to the structure of the original linked list constantly create new nodes, and maintain a good next pointer, the correspondence between Node1 and Node2 saved to Hash_map, * in case of the subsequent maintenance of the random pointer, you can node1 to find the corresponding Node2         。 */         while(Node1->next! =NULL) {Mp[node1]=Node2; Node1= node1->Next; Node2->next =NewRandomlistnode (node1->label); Node2= node2->Next; }        //The correspondence between the tails of the two lists is also preserved .MP[NODE1] =Node2; //continue processing the random pointer from the beginningNode1 =Head; Node2=New_head;  while(Node1->next! =NULL) {Node2->random = mp[node1->Random]; Node1= node1->Next; Node2= node2->Next; }        //handle the random hands of the tail, too.Node2->random = mp[node1->Random]; returnNew_head; }};

Methods that do not use Hash_map:

/** Definition for singly-linked list with a random pointer. * struct RANDOMLISTNODE {* int label; * Randomli Stnode *next, *random; * Randomlistnode (int x): label (x), Next (null), random (null) {} *}; */classSolution { Public: Randomlistnode*copyrandomlist (Randomlistnode *head) {        /** * Hypothesis: L1 represents a node in the original list; L2 represents a node in a new linked list*/Randomlistnode*new_head, *L1, *L2; if(head = = NULL)returnNULL; /** * First step: Create a L2 behind each L1, and let L1 point to L2,l2 point to the next L1;*/         for(L1 = head; L1! = NULL; l1 = l1->next->next) {L2=NewRandomlistnode (l1->label); L2->next = l1->Next; L1->next =L2; }                /** Second step: Give L2 random value, L1 's random next point is L2 's random target;*/New_head= head->Next;  for(L1 = head; L1! = NULL; l1 = l1->next->next) {            if(L1->random! = NULL) L1->next->random = l1->random->Next; }                /** Step Three: You need to split the entire list into two linked lists, specifically to let L1 next point to the back; * L2 's next also points behind. */         for(L1 = head; L1! = NULL; l1 = l1->next) {L2= l1->Next; L1->next = l2->Next; if(L2->next! = NULL) L2->next = l2->next->Next; }        returnNew_head; }};

"Leetcode" 138. 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.