Clone a Linked List with Random Pointers

Given a linked list where each node has two pointers: the first points to the next node in the list, and the second is a random pointer that can point to any node in the list or be null. Your task is to create a deep copy of this linked list and return the head of the copied list. The deep copy should not use references of the original nodes but create new nodes for the copy.

Example:

Input:
T = 1
Linked list: 1 2 2 0 3 4 4 4 5 1 -1
Output:
True
Explanation:

The input describes a linked list where each node is represented by a pair of value and random index. For instance, '1 2' means node value 1 with random pointer to the node at index 2. '-1' signifies the end of the list. If the linked list is successfully cloned, the output will be "True".

Constraints:

  • 1 <= T <= 10
  • 0 <= N <= 5 * 104
  • -105 <= data <= 105 and data != -1
  • -1 <= random index < N
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
AnswerBot
4mo

Create a deep copy of a linked list with random pointers without using references of original nodes.

  • Iterate through the original linked list and create a new node for each node in the list.

  • Store the m...read more

Help your peers!
Select
Add answer anonymously...

Popular interview questions of Web Developer

A Web Developer was asked Q1. What are pointers in C?
A Web Developer was asked Q2. What is 'str' in C?
A Web Developer was asked Q3. Can you design a website similar to Amazon?
Internshala Web Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits