Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Clone List with Random Pointer Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Clone List With Random Pointer

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

You are given a linked list L1 of length n. Each node contains an additional random pointer which could point to any of the nodes in the list, or null.

You need to create a deep copy of this list with n newly created nodes. This will create a new linked list L2. All nodes of L2 will have the same value as the corresponding node in L1. The next and random pointers will point to nodes in L2 rather than L1.

Return the head of L2.

Approach

  • Traverse the given linked list and for every node create its copy and insert it just after the original node in the linked list.
  • Now again traverse the modified list to assign the random pointer of each copy node. This can be done as currNode->next->random = currNode->random->next.
  • This method works because currNode->next points to the copy node of the original node and currNode->random->next points to the copy node of the random pointer of the original node.
  • Now traverse the list for the third time and extract the copied nodes from it to make a copy of the given linked list.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
/* This is the ListNode class definition

class ListNode {
    public:
    int data;
    ListNode *next;
    ListNode* random;
    ListNode (int data) {
        this->data = data;
        this->next = NULL;
        this->random = NULL;
    }
};
*/

ListNode* cloneTheLinkedList (ListNode* head) {
    ListNode* currNode = head;
	while (currNode != NULL) {
		ListNode* copyNode = new ListNode(currNode->data);
		copyNode->next = currNode->next;
		currNode->next = copyNode;
		currNode = currNode->next->next;	
	}
	currNode = head;
	while (currNode != NULL) {
		if (currNode->random != NULL) {
			currNode->next->random = currNode->random->next;
		}
		currNode = currNode->next->next;
	}
	head = head->next;
	currNode = head;
	while (currNode->next != NULL) {
		currNode->next = currNode->next->next;
		currNode = currNode->next;
	}
	return head;
}
Java
/** This is the ListNode class definition

class ListNode {
	int data;
	ListNode next;
    ListNode random;

	ListNode (int data) {
        this.data = data;
        this.next = null;
        this.random = null;
    }
}
**/
class Solution {
	ListNode cloneTheLinkedList(ListNode head) {
		ListNode currNode = head;
		while (currNode != null) {
			ListNode copyNode = new ListNode(currNode.data);
			copyNode.next = currNode.next;
			currNode.next = copyNode;
			currNode = currNode.next.next;	
		}
		currNode = head;
		while (currNode != null) {
			if (currNode.random != null) {
				currNode.next.random = currNode.random.next;
			}
			currNode = currNode.next.next;
		}
		head = head.next;
		currNode = head;
		while (currNode.next != null) {
			currNode.next = currNode.next.next;
			currNode = currNode.next;
		}
		return head;
	}
}

Another Approach

  • Traverse the given linked list and for every node create its copy and store the original and copy node in a hashmap.
  • Since all the copy nodes are now present in the hashmap, simply traverse the linked list again and for every value in the hashmap assign the next and random pointers of the copy nodes using the next and random pointers of the original nodes.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(n)

Implementation

C++
/* This is the ListNode class definition

class ListNode {
    public:
    int data;
    ListNode *next;
    ListNode* random;
    ListNode (int data) {
        this->data = data;
        this->next = NULL;
        this->random = NULL;
    }
};
*/

ListNode* cloneTheLinkedList (ListNode* head) {
    unordered_map<ListNode*, ListNode*> copyNodes;
	ListNode* currNode = head;
	while(currNode != NULL) {
		copyNodes[currNode] = new ListNode(currNode->data);
		currNode = currNode->next;
	}
	currNode = head;
	while(currNode != NULL) {
		copyNodes[currNode]->next = copyNodes[currNode->next];
		copyNodes[currNode]->random = copyNodes[currNode->random];
		currNode = currNode->next;
	}
	return copyNodes[head];
}
Java
/** This is the ListNode class definition

class ListNode {
	int data;
	ListNode next;
    ListNode random;

	ListNode (int data) {
        this.data = data;
        this.next = null;
        this.random = null;
    }
}
**/
class Solution {
	ListNode cloneTheLinkedList(ListNode head) {
		HashMap<ListNode, ListNode> copyNodes = new HashMap<ListNode, ListNode>();
		ListNode currNode = head;
		while(currNode != null) {
			copyNodes.put(currNode, new ListNode(currNode.data));
			currNode = currNode.next;
		}
		currNode = head;
		while(currNode != null) {
			copyNodes.get(currNode).next = copyNodes.get(currNode.next);
			copyNodes.get(currNode).random = copyNodes.get(currNode.random);
			currNode = currNode.next;
		}
		return copyNodes.get(head);
	}
}
Related Content
Add Element at Kth Position in Linked List
Add Two Numbers as Lists
Add One to Linked List
Append Linked Lists
Remove Element at Kth Position in Linked List
Delete Node From Linked List
Delete Xth Node From End of Linked List
Detect Loop in Linked List
Distinct Numbers in Window
Find xth Node from End of Linked List
Flatten a Multi-Level Linked List
Four Sum
Identical Twins
Implement Counting Sort
Insertion Sort Linked List
Intersection of Two Linked Lists
Kth Element in Linked List
Linked List Palindrome
Linked List to Array
Longest Consecutive Sequence
Longest Subarray with Zero Sum
Longest Substring with K Unique Characters
Longest Substring Without Repeat
LRU Cache
Merge Sort Linked List
Merge Two Sorted Linked List
Middle Element of Linked List
Non-Repeating Element
Partition List
Primes upto N
Print Linked List
Print Reversed Linked List
Remove Duplicates from Sorted Linked List
Remove Duplicates from Sorted Linked List - II
Remove Loop From Linked List
Remove occurrences in Linked List
Reorder List
Reverse a Linked List
Reverse a Linked List II
Reverse a Linked List in k-groups
Rotate a Linked List
Subarrays With Given XOR
Three Sum
Two Sum
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet