C++ Program For Cloning A Linked List With Next And Random Pointer- Set 2 Last Updated : 27 Dec, 2021 Comments Improve Suggest changes Like Article Like Report We have already discussed 2 different ways to clone a linked list. In this post, one more simple method to clone a linked list is discussed. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. The idea is to use Hashing. Below is algorithm. Traverse the original linked list and make a copy in terms of data. Make a hash map of key value pair with original linked list node and copied linked list node. Traverse the original linked list again and using the hash map adjust the next and random reference of cloned linked list nodes. Below is the implementation of above approach. C++ // C++ program to clone a linked list // with random pointers #include<bits/stdc++.h> using namespace std; // Linked List Node class Node { public: // Node data int data; // Next and random reference Node *next, *random; Node(int data) { this->data = data; this->next = this->random = NULL; } }; // Linked list class class LinkedList { public: // Linked list head reference Node *head; LinkedList(Node *head) { this->head = head; } // Push method to put data always at // the head in the linked list. void push(int data) { Node *node = new Node(data); node->next = head; head = node; } // Method to print the list. void print() { Node *temp = head; while (temp != NULL) { Node *random = temp->random; int randomData = ((random != NULL) ? random->data : -1); cout << "Data = " << temp->data << ", "; cout << "Random Data = " << randomData << endl; temp = temp->next; } cout << endl; } // Actual clone method which returns // head reference of cloned linked // list. LinkedList* clone() { // Initialize two references, // one with original list's head. Node *origCurr = head; Node *cloneCurr = NULL; // Hash map which contains node // to node mapping of original // and clone linked list. unordered_map<Node*, Node*> mymap; // Traverse the original list and // make a copy of that in the // clone linked list. while (origCurr != NULL) { cloneCurr = new Node(origCurr->data); mymap[origCurr] = cloneCurr; origCurr = origCurr->next; } // Adjusting the original list // reference again. origCurr = head; // Traversal of original list again // to adjust the next and random // references of clone list using // hash map. while (origCurr != NULL) { cloneCurr = mymap[origCurr]; cloneCurr->next = mymap[origCurr->next]; cloneCurr->random = mymap[origCurr->random]; origCurr = origCurr->next; } // return the head reference of // the clone list. return new LinkedList(mymap[head]); } }; // Driver code int main() { // Pushing data in the linked list. LinkedList *mylist = new LinkedList(new Node(5)); mylist->push(4); mylist->push(3); mylist->push(2); mylist->push(1); // Setting up random references. mylist->head->random = mylist->head->next->next; mylist->head->next->random = mylist->head->next->next->next; mylist->head->next->next->random = mylist->head->next->next->next->next; mylist->head->next->next->next->random = mylist->head->next->next->next->next->next; mylist->head->next->next->next->next->random = mylist->head->next; // Making a clone of the original // linked list. LinkedList *clone = mylist->clone(); // Print the original and cloned // linked list. cout << "Original linked list"; mylist->print(); cout << "Cloned linked list"; clone->print(); } // This code is contributed by Chhavi Output: Original linked list Data = 1, Random data = 3 Data = 2, Random data = 4 Data = 3, Random data = 5 Data = 4, Random data = -1 Data = 5, Random data = 2 Cloned linked list Data = 1, Random data = 3 Data = 2, Random data = 4 Data = 3, Random data = 5 Data = 4, Random data = -1 Data = 5, Random data = 2 Time complexity: O(n) Auxiliary space: O(n) Please refer complete article on Clone a linked list with next and random pointer | Set 2 for more details! Comment More infoAdvertise with us Next Article C++ Program For Cloning A Linked List With Next And Random Pointer- Set 2 kartik Follow Improve Article Tags : Linked List C++ Programs DSA Linked Lists Microsoft Amazon Morgan Stanley BankBazaar MakeMyTrip Ola Cabs +6 More Practice Tags : AmazonBankBazaarMakeMyTripMicrosoftMorgan StanleyOla CabsLinked List +3 More Similar Reads C++ Program For Writing A Function To Get Nth Node In A Linked List Write a C++ Program to GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before m 4 min read C++ Program For Printing Nth Node From The End Of A Linked List Given a Linked List and a number n, write a function that returns the value at the n'th node from the end of the Linked List.For example, if the input is below list and n = 3, then output is "B" Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Method 1 (Use length 4 min read C++ Program For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit 2 min read C++ Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15- 5 min read C++ Program For Searching An Element In A Linked List Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functi 4 min read C++ Program For Inserting A Node In A Linked List Inserting a node into a linked list can be done in several ways, depending on where we want to insert the new node. Here, we'll cover four common scenarios: inserting at the front of the list, after a given node, at a specific position, and at the end of the listTable of ContentInsert a Node at the 9 min read C++ Program For Moving All Occurrences Of An Element To End In A Linked List Given a linked list and a key in it, the task is to move all occurrences of the given key to the end of the linked list, keeping the order of all other elements the same. Examples: Input : 1 -> 2 -> 2 -> 4 -> 3 key = 2 Output : 1 -> 4 -> 3 -> 2 -> 2 Input : 6 -> 6 -> 7 6 min read Correct the Random Pointer in Doubly Linked List Given a doubly linked list having exactly one of the node pointing to a random node in the list, the task is to correct this random pointer in the doubly linked list, such that it points to the expected node.Examples: Input: Output: Explanation: 2's next pointer has been corrected to point to 3. Ear 8 min read C++ Program For Writing A Function To Delete A Linked List Algorithm For C++:Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted. Implementation: C++ // C++ program to delete a linked list #include <bits/stdc++.h> using namespace std 2 min read C++ Program For Finding Length Of A Linked List Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1->3->1->2->1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initia 4 min read Like