Python Program For Cloning A Linked List With Next And Random Pointer- Set 2 Last Updated : 18 May, 2022 Summarize Comments Improve Suggest changes Share 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. Python3 # Python3 program to clone a linked list # with random pointers class Node: def __init__(self, data): # Node Data self.data = data # Node Next self.next = None # Node Random self.random = None # Dictionary class MyDictionary(dict): # __init__ function def __init__(self): super().__init__() self = dict() # Function to add key:value def add(self, key, value): # Adding Values to dictionary self[key] = value # Linked list class class LinkedList: # Linked list constructor def __init__(self, node): self.head = node # Method to print the list. def __repr__(self): temp = self.head while temp is not None: random = temp.random random_data = (random.data if random is not None else -1) data = temp.data print(f"Data-{data}, Random data: {random_data}") temp = temp.next return "" # Push method to put data always at # the head in the linked list. def push(self, data): node = Node(data) node.next = self.head self.head = node # Actual clone method which returns head # reference of cloned linked list. def clone(self): # Initialize two references, one # with original list's head. original = self.head clone = None # Initialize two references, one # with original list's head. mp = MyDictionary() # Traverse the original list and # make a copy of that # in the clone linked list while original is not None: clone = Node(original.data) mp.add(original, clone) original = original.next # Adjusting the original # list reference again. original = self.head # Traversal of original list again # to adjust the next and random # references of clone list using hash map. while original is not None: clone = mp.get(original) clone.next = mp.get(original.next) clone.random = mp.get(original.random) original = original.next # Return the head reference of the clone list. return LinkedList(self.head) # Driver code # Pushing data in the linked list. l = LinkedList(Node(5)) l.push(4) l.push(3) l.push(2) l.push(1) # Setting up random references. l.head.random = l.head.next.next l.head.next.random = l.head.next.next.next l.head.next.next.random = l.head.next.next.next.next l.head.next.next.next.random = (l.head.next.next.next.next.next) l.head.next.next.next.next.random = l.head.next # Making a clone of the # original linked list. clone = l.clone() # Print the original and cloned # linked list.s print("Original linked list") print(l) print("Cloned linked list") print(clone) # This code is contributed by ashwinathappan 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 Python Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links K kartik Follow Improve Article Tags : Linked List Python Programs DSA Linked Lists Microsoft Amazon Morgan Stanley BankBazaar MakeMyTrip Ola Cabs Python-DSA +7 More Practice Tags : AmazonBankBazaarMakeMyTripMicrosoftMorgan StanleyOla CabsLinked List +3 More Similar Reads Python 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 Python 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 Python 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: 3 min read Python 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 Python 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 Python Program For Sorting A Linked List Of 0s, 1s And 2s Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 1 -> 1 -> 2 -> 0 -> 2 -> 0 -> 1 -> NULL Output: 0 -> 0 -> 1 -> 1 -> 1 -> 2 -> 2 -> NULL Input: 1 -> 1 -> 2 -> 1 -> 0 -> NULLÂ Output: 0 -> 1 -> 1 -> 1 -> 2 -> NULL Source: Microsoft Interview | Set 1 Recommended: Please solve it on "PRAC 3 min read Like