Cloning Linked List With Random Pointers And Next

easy
1. You are given a linked list with N nodes.
   2. Each Node has two pointers.
   3. One pointer points to the next node as usual.
   4. The other pointer points to any random node in the linked list.
   5. You have to write a function that takes input as head of the given list, clones it and returns the head of the cloned linked list.
   6. This is a functional problem.
   7. You should code only the copyList function, feel free to change its signature if required. It should return the head of cloned the linked list.
   8. Node class represents the node of Linked List.
   9. main takes input from the users and creates the Linked List.
   10. Don't change the code of the main and other functions defined already.

Input Format

Input is handled for you. First line takes N and M. N denotes the number of elements in the list. M denotes number of nodes having random pointers. Next line takes input N space separated integers for values of the nodes. Next line contains M pairs denoting the random pointer from ith node to jth node.

Output Format

Output is handled for you. The cloned linked list, if cloning is correct. Else the output is "false".

Constraints

1 <= N <= 100
   M <= N
   -100 <= L[i] <= 100

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
4 2                                       
1 2 3 4                             
1 2 2 4
Output
1 2 3 4
Previous
Playing the Game
Next
Maximum Rectangle Area In Histogram

Related Questions