BETA

Difficulty Level: Medium

Submissions: 3477 Accuracy:

29.97%

Clone a linked list with next and random pointer (Function Problem)

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

 

ArbitLinked List1

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.
 

Input:
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.

Output:
Your function should return the head of the copied 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                                
The first line above represents T, i.e., the number of test cases. The second line is of the form N Q, where N is the number of nodes and Q is the number of nodes with arbitrary pointers. Third line presents all the nodes of the list. The fourth line represents Q pairs of integers in the order (a b) where a is the pointer and b is the node being pointed to (See explanation below for more understanding).

Explanation of the example: There is one test case. 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.

Output:
1    
The output is 1 if linked list is successfully cloned, else false.

Note : The Input/Output format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console, and should not print anything on stdout/console. The task is to complete the function specified, and not to write the full code.
 

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


					   
It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result:

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