Java 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. Java // Java program to clone a linked list // with random pointers import java.util.HashMap; import java.util.Map; // Linked List Node class class Node { // Node data int data; // Next and random reference Node next, random; // Node constructor public Node(int data) { this.data = data; this.next = this.random = null; } } // Linked list class class LinkedList { // Linked list head reference Node head; // Linked list constructor public LinkedList(Node head) { this.head = head; } // Push method to put data always // at the head in the linked list. public void push(int data) { Node node = new Node(data); node.next = this.head; this.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); System.out.println("Data = " + temp.data + ", Random data = " + randomData); temp = temp.next; } } // Actual clone method which returns head // reference of cloned linked list. public LinkedList clone() { // Initialize two references, one with // original list's head. Node origCurr = this.head, cloneCurr = null; // Hash map which contains node to node // mapping of original and clone linked list. Map<Node, Node> map = new HashMap<Node, Node>(); // Traverse the original list and make a // copy of that in the clone linked list. while (origCurr != null) { cloneCurr = new Node(origCurr.data); map.put(origCurr, cloneCurr); origCurr = origCurr.next; } // Adjusting the original list // reference again. origCurr = this.head; // Traversal of original list again to // adjust the next and random references // of clone list using hash map. while (origCurr != null) { cloneCurr = map.get(origCurr); cloneCurr.next = map.get(origCurr.next); cloneCurr.random = map.get(origCurr.random); origCurr = origCurr.next; } // Return the head reference of the // clone list. return new LinkedList(map.get(this.head)); } } // Driver Class class Main { // Main method. public static void main(String[] args) { // Pushing data in the linked list. LinkedList list = new LinkedList(new Node(5)); list.push(4); list.push(3); list.push(2); list.push(1); // Setting up random references. list.head.random = list.head.next.next; list.head.next.random = list.head.next.next.next; list.head.next.next.random = list.head.next.next.next.next; list.head.next.next.next.random = list.head.next.next.next.next.next; list.head.next.next.next.next.random = list.head.next; // Making a clone of the original // linked list. LinkedList clone = list.clone(); // Print the original and cloned // linked list. System.out.println( "Original linked list"); list.print(); System.out.println( "Cloned linked list"); clone.print(); } } 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 Java Program For Cloning A Linked List With Next And Random Pointer- Set 2 kartik Follow Improve Article Tags : Linked List Java Programs DSA Linked Lists Microsoft Amazon Morgan Stanley BankBazaar MakeMyTrip Ola Cabs +6 More Practice Tags : AmazonBankBazaarMakeMyTripMicrosoftMorgan StanleyOla CabsLinked List +3 More Similar Reads Java Program For Cloning A Linked List With Next And Random Pointer In O(1) Space Given a linked list having two pointers in each node. The first one points to the next node of the list, however, the other pointer is random and can point to any node of the list. Write a program that clones the given list in O(1) space, i.e., without any extra space. Examples: Input : Head of the 4 min read Java Program For Selecting A Random Node From A Singly Linked List Given a singly linked list, select a random node from the linked list (the probability of picking a node should be 1/N if there are N nodes in the list). You are given a random number generator.Below is a Simple Solution: Count the number of nodes by traversing the list.Traverse the list again and s 4 min read Java Program to Reverse a Linked List Without Manipulating its Pointers Given a linked list, the task is to write a program in Java that reverses the linked list without manipulating its pointers, i.e., the reversal should happen just by changing the data values and not the links. ExamplesInput: Original linked list 1->2->3->4->5->nullOutput: Linked list 3 min read Java Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 2->1->2->1->1->2->0->1->0 Output: 0->0->1->1->1->1->2->2->2 The sorted Array is 0, 0, 1, 1, 1, 1, 2, 2, 2. Input: 2->1->0 Output: 0->1->2 The sorted Array is 0, 1, 2Recommended: Please solve it on "PRACTICE" first, before moving on to th 3 min read Java 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 the list and n = 3, then the output is "B". Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.Method 1 (Use 5 min read Java Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1 3 min read Java Program to Create a Singly Linked List and Count the Number of Nodes Linked List is a linear data structure. Linked list elements are not stored at a contiguous location, the elements are linked using pointers. Singly Linked list is the collection of nodes, where each node has two parts one is the data and other is the linked part. Example: Input : AddNodes = {2, 3, 3 min read Java Program For Deleting A Node In A Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read Java 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- 6 min read Java Program For Printing Nth Node From The End Of A Linked List(Duplicate) 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 2 min read Like