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.


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

Popular interview questions of SDE-2
Reviews
Interviews
Salaries
Users/Month