Asked inDP World,SDE-2

Clone a Linked List with Random Pointers

You are provided with a linked list where each node has two pointers. The first pointer directs to the next node, and the second one, known as the 'random' pointer, 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. We will validate if the copied linked list is a true deep copy of the original.

Example:

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

For the given list, each node contains a value and a random index indicating which node the random pointer directs to. If the random pointer is null, it is represented as -1. For instance, the input indicates that the node with value 1 has its random pointer pointed to the node at index 2.

Constraints:

  • 1 ≤ T ≤ 10
  • 0 ≤ N ≤ 5 * 104
  • -105 ≤ data ≤ 105 and data ≠ -1
  • -1 ≤ random index < N
Note:
You are not required to print anything; it has already been handled. Implement the function as directed.
AnswerBot
4mo

Clone a linked list with random pointers and return the head of the copied list.

  • Create a deep copy of the linked list by iterating through each node and creating a new node with the same value.

  • Update ...read more

Help your peers!
Select
Add answer anonymously...
DP World SDE-2 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