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.
Be the first one to answer
Add answer anonymously...
Top Internshala Web Developer interview questions & answers
Popular interview questions of Web Developer
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