You are given a Linked List with N nodes with M nodes having random pointers pointing to any other node consisting of two pointers in each node, with one pointer of each node pointing to the next node just like in a single linked 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
Input:
First line of input contains number of testcases T. For each testcase, first line of input contains two integers N and M. Next line of input contains values of N nodes of the linked list and last line contains the nodes, for which each ith node is connected to any jth node.

Output:
For each testcase, clone the given linked list.

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

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

Example:
Input:

1           
4 2                                       
1 2 3 4                             
1 2 2 4

Output:
1

Explanation:
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


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



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

to report an issue on this page.