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
anddata ≠ -1
-1 ≤ random index < N
Note:
You are not required to print anything; it has already been handled. Implement the function as directed.
Be the first one to answer
Add answer anonymously...
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app