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.
Input : Head of the below-linked list Output : A new linked list identical to the original list.
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 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 copy of original and Original->random->next is nothing but copy of 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.
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
This article is contributed by Ashutosh Kumar 😀 If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Clone a linked list with next and random pointer | Set 1
- Clone a linked list with next and random pointer | Set 2
- Insertion in a sorted circular linked list when a random pointer is given
- Correct the Random Pointer in Doubly Linked List
- Point to next higher value node in a linked list with an arbitrary pointer
- Clone a stack without extra space
- Length of longest palindrome list in a linked list using O(1) extra space
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- How to write C functions that modify head pointer of a Linked List?
- Point arbit pointer to greatest value right side node in a linked list
- Delete multiple occurrences of key in Linked list using double pointer
- Delete a Node from linked list without head pointer
- Print reverse of a Linked List without extra space and modifications
- Difference between Singly linked list and Doubly linked list
- Select a Random Node from a Singly Linked List
- Sum of nodes in a linked list which are greater than next node
- Next greater element in the Linked List
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2