Clone a linked list with next and random pointer
Submissions: 7407   Accuracy:


  Difficulty: Medium   Marks: 4

You are given a Link List consisting of two pointers in each node, with one pointer of each node pointing to the next node just like in a single link list and the second pointer which is arbitary pointer can point to any node in the list and not just the previous node.  


ArbitLinked List1
You have to complete the method which takes one argument: the head of the linked list. You should not read any input from stdin/console.
The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list and an arbitrary pointer (arb) which points to any arbitrary node .
There are multiple test cases. For each test case, this method will be called individually.

For each testcase, clone the given linked list.

User Task:
The task is to complete the function copyList() which take one argument the head of the linked list to be copied and should return the head of the copied linked list.

1 <= T <= 100
1 <= N <= 100
1 <= Q <= 100
1 <= a, b <= 100


4 2                                       
1 2 3 4                             
1 2 2 4


Testcase 1: In this test case, there are 4 nodes in linked list.  Among these 4 nodes,  2 nodes have arbit pointer set, rest two nodes have arbit pointer as NULL. Third line tells us the value of four nodes. The fourth line gives the information about arbitrary pointers. The first node with set arbit pointer is 1, its arbit pointer is pointing to node 2.  The second node with set arbit pointer is 2, its arbit pointer is pointing to node 4.


** For More Input/Output Examples Use 'Expected Output' option **

Author: Shubham Joshi 1

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.