Clone the Linked List with Random and Next Pointer in Python
A linked list is created using a random pointer. Given an N-by-N linked list, where each node has two connections, one pointing to the node after it and the other to any node in the list. We have to duplicate this linked list in O(N) time.
Recall that the "next" pointer points to the next node in the linked list, but the "random" pointer might point to any node in the linked list at random.
Approach - 1
The new nodes are then mapped to their corresponding nodes in the existing linked list by first creating a singly linked list with just the 'next' pointer. You may now use any node in the freshly formed list to point to the arbitrary node using this mapping.
Here is the algorithmic approach to the above idea:
The initial linked list is as follows: 2(9) -> 6(2) -> 9(15) -> 13(9) -> 15(6) 2 : 2, 6 : 6, 9 : 9, 13 : 13, 15 : 15 The clone of the linked list is: 2(9) -> 6(2) -> 9(15) -> 13(9) -> 15(6)
Time Complexity: The time complexity of this program is O(N), where N is the number of nodes in the linked list. We have not used any nested loop and hence traversed the linked list linearly; therefore, the time complexity is also linear.
Auxiliary Space: The space complexity of the program is also O(N). The space complexity is linear because we are creating a new linked list.
Approach - 2 (Optimizing the Space Complexity)
Below is the method to optimize the space complexity of the program:
We will create a duplicate node and store it in between the actual node and the node next to it.
This implies that for a node N, we will create a node having the same value as N and store it as N -> next. This node will serve as the duplicate node. The random pointer of the duplicate node will point to the N -> random -> next, as it is the duplicate of N -> random.
Below is the algorithmic approach to the above idea:
The initial linked list is as follows: 2 ( 9 ) -> 6 (2) -> 9 (15) -> 13 (9) -> 15 (6) The clone of the linked list is: 2 ( 9 ) -> 6 (2) -> 9 (15) -> 13 (9) -> 15 (6)
Time Complexity: The time complexity of this program is also O(N). We are linearly traversing through the linked list, and hence the time complexity is also linear.
Auxiliary Space: Since we have not created any mapping in this approach and only stored the node objects in the original linked list therefore, the space complexity of this approach is constant. The space complexity is O(1).
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week