Java Program For Cloning A Linked List With Next And Random Pointer In O(1) Space
Last Updated :
22 Jun, 2022
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.
Java
// Java program to clone a linked list with next
// and arbit pointers in O(n) time
class GfG {
// Structure of linked list Node
static class Node {
int data;
Node next, random;
Node(int x)
{
data = x;
next = random = null;
}
}
// Utility function to print the list.
static void print(Node start)
{
Node ptr = start;
while (ptr != null) {
System.out.println("Data = " + ptr.data
+ ", Random = "
+ ptr.random.data);
ptr = ptr.next;
}
}
// This function clones a given
// linked list in O(1) space
static Node clone(Node start)
{
Node curr = start, temp = null;
// insert additional node after
// every node of original list
while (curr != null) {
temp = curr.next;
// Inserting node
curr.next = new Node(curr.data);
curr.next.next = temp;
curr = temp;
}
curr = start;
// adjust the random pointers of the
// newly added nodes
while (curr != null) {
if (curr.next != null)
curr.next.random = (curr.random != null)
? curr.random.next
: curr.random;
// move to the next newly added node by
// skipping an original node
curr = curr.next.next;
}
Node original = start, copy = start.next;
// save the start of copied linked list
temp = copy;
// now separate the original list and copied list
while (original != null) {
original.next =original.next.next;
copy.next = (copy.next != null) ? copy.next.next
: copy.next;
original = original.next;
copy = copy.next;
}
return temp;
}
// Driver code
public static void main(String[] args)
{
Node start = new Node(1);
start.next = new Node(2);
start.next.next = new Node(3);
start.next.next.next = new Node(4);
start.next.next.next.next = new Node(5);
// 1's random points to 3
start.random = start.next.next;
// 2's random points to 1
start.next.random = start;
// 3's and 4's random points to 5
start.next.next.random = start.next.next.next.next;
start.next.next.next.random
= start.next.next.next.next;
// 5's random points to 2
start.next.next.next.next.random = start.next;
System.out.println("Original list : ");
print(start);
System.out.println("Cloned list : ");
Node cloned_list = clone(start);
print(cloned_list);
}
}
// This code is contributed by Prerna Saini.
OutputOriginal 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!
Similar Reads
Java Program For Cloning A Linked List With Next And Random Pointer- Set 2 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 link
4 min read
Java Program For Pointing To Next Higher Value Node In A Linked List With An Arbitrary Pointer Given singly linked list with every node having an additional "arbitrary" pointer that currently points to NULL. Need to make the "arbitrary" pointer point to the next higher value node. We strongly recommend to minimize your browser and try this yourself first A Simple Solution is to traverse all n
4 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 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 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 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 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 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
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 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