Clone Linked List with Random Pointer Problem Statement

Given a linked list where each node has two pointers: one pointing to the next node and another which can point randomly to any node in the list or null, your task is to create a deep copy of this linked list and return its head. The validation will be done to ensure the cloned linked list is an exact copy of the original.

Explanation:

A deep copy means creating new nodes rather than copying references of the original linked list's nodes.

Example:

Consider the following illustration where random pointers are shown in red and next pointers in black:

example

Input:

The first line contains an integer ‘T’, the number of test cases.
Each test case consists of a single line with nodes (value and random pointer index) separated by spaces. -1 is used for null values.
Each node is represented as a pair:
- Value: an integer for node value
- Random index: index for the node the random pointer points to or -1 if it's null

Output:

For each test case, output "true" if the linked list is successfully cloned.
Each output is on a new line for separate test cases.

Constraints:

  • 1 <= T <= 10^2
  • 0 <= N <= 10^3 (where N is the number of nodes)
  • 0 <= DATA <= 10^6 and DATA != -1
  • -1 <= RANDOMINDEX < N

Each node is represented by [value, randomIndex] format, end of the list denoted by [-1].

Note:

You are required to implement the function; printing is handled separately.

Follow-up:

Can the cloning be accomplished without utilizing extra space?
AnswerBot
4mo

Yes, the cloning of a linked list with random pointer can be accomplished without utilizing extra space.

  • Use a hashmap to store the mapping between original nodes and their corresponding cloned nodes.

  • I...read more

Help your peers!
Select
Add answer anonymously...

Times Internet Software Developer interview questions & answers

A Software Developer was asked Q. Write a function to delete a node in a singly linked list, given only access to ...read more
A Software Developer was asked Q. Which data structure would you use to program a jigsaw puzzle program, and what ...read more
A Software Developer was asked Q. What is polymorphism? Can you provide some examples?

Popular interview questions of Software Developer

A Software Developer was asked Q1. Write a function to delete a node in a singly linked list, given only access to ...read more
A Software Developer was asked Q2. Which data structure would you use to program a jigsaw puzzle program, and what ...read more
A Software Developer was asked Q3. What is polymorphism? Can you provide some examples?
Times Internet Software 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