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 below-linked list
Output : A new linked list identical to the original list.
In the previous posts Set-1 and Set-2 various methods are discussed, and O(n) space complexity implementation is also available.
In this post, we’ll be implementing an algorithm that’d require no additional space as discussed in Set-1.
Below is the Algorithm:
- Create the copy of node 1 and insert it between node 1 & node 2 in the original Linked List, create a copy of 2 and insert it between 2 & 3. Continue in this fashion, add the copy of N after the Nth node
- Now copy the random link in this fashion
original->next->random= original->random->next; /*TRAVERSE TWO NODES*/
- This works because original->next is nothing but a copy of the original and Original->random->next is nothing but a copy of the random.
- Now restore the original and copy linked lists in this fashion in a single loop.
original->next = original->next->next; copy->next = copy->next->next;
- Ensure that original->next is NULL and return the cloned list
Below is the implementation.
'''Python program to clone a linked list with next and arbitrary pointers''' '''Done in O(n) time with O(1) extra space''' class Node:
'''Structure of linked list node'''
def __init__( self , data):
self .data = data
self . next = None
self .random = None
def clone(original_root):
'''Clone a doubly linked list with random pointer'''
'''with O(1) extra space'''
'''Insert additional node after every node of original list'''
curr = original_root
while curr ! = None :
new = Node(curr.data)
new. next = curr. next
curr. next = new
curr = curr. next . next
'''Adjust the random pointers of the newly added nodes'''
curr = original_root
while curr ! = None :
curr. next .random = curr.random. next
curr = curr. next . next
'''Detach original and duplicate list from each other'''
curr = original_root
dup_root = original_root. next
while curr. next ! = None :
tmp = curr. next
curr. next = curr. next . next
curr = tmp
return dup_root
def print_dlist(root):
'''Function to print the doubly linked list'''
curr = root
while curr ! = None :
print ( 'Data =' , curr.data, ', Random =' , curr.random.data)
curr = curr. next
####### Driver code ####### '''Create a doubly linked list''' original_list = Node( 1 )
original_list. next = Node( 2 )
original_list. next . next = Node( 3 )
original_list. next . next . next = Node( 4 )
original_list. next . next . next . next = Node( 5 )
'''Set the random pointers''' # 1's random points to 3 original_list.random = original_list. next . next
# 2's random points to 1 original_list. next .random = original_list
# 3's random points to 5 original_list. next . next .random = original_list. next . next . next . next
# 4's random points to 5 original_list. next . next . next .random = original_list. next . next . next . next
# 5's random points to 2 original_list. next . next . next . next .random = original_list. next
'''Print the original linked list''' print ( 'Original list:' )
print_dlist(original_list) '''Create a duplicate linked list''' cloned_list = clone(original_list)
'''Print the duplicate linked list''' print ('
Cloned list :')
print_dlist(cloned_list) '''This code is contributed by Shashank Singh''' |
Original list : Data = 1, Random = 3 Data = 2, Random = 1 Data = 3, Random = 5 Data = 4, Random = 5 Data = 5, Random = 2 Cloned list : Data = 1, Random = 3 Data = 2, Random = 1 Data = 3, Random = 5 Data = 4, Random = 5 Data = 5, Random = 2
Time Complexity: O(n), where n is the number of nodes in the given linked list.
Auxiliary Space: O(1), as no extra space is used. The n nodes which are inserted in between the nodes was already required to clone the list, so we can say that we did not use any extra space.
Please refer complete article on Clone a linked list with next and random pointer in O(1) space for more details!