A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

For example, if there are two nodes X and Y in the original list, where X.random --&gt; Y, then for the corresponding two nodes x and y in the copied list, x.random --&gt; y.

Return the head of the copied linked list.

The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:


	val: an integer representing Node.val
	random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.


Your code will only be given the head of the original linked list.

&nbsp;
Example 1:

Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]


Example 2:

Input: head = [[1,1],[2,1]]
Output: [[1,1],[2,1]]


Example 3:



Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]]


&nbsp;
Constraints:


	0 &lt;= n &lt;= 1000
	-104 &lt;= Node.val &lt;= 104
	Node.random is null or is pointing to some node in the linked list.


from collections import defaultdict

"""
# Definition for a Node.
class Node:
    def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
        self.val = int(x)
        self.next = next
        self.random = random
"""

class Solution:
    def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
        
        
        
        # ll=res
        # k=0
        # ind=0
        # kmk=1
        jj=None
        g=defaultdict(lambda:-1)
        k=0
        hh=head
        while(head):
            res=Node(head.val)
            if k==0:
                jj=res

            if k==1:
                prev.next=res
            
            g[head]=res
            prev=res
            k=1
            head=head.next
            
        # print(g)
        
        # for i in g:
        #     print(i.val,g[i].val)
            
        kk=jj
        mm=jj
        # print(head)
        while(hh):
            if hh.random!=None:
                
                jj.random=g[hh.random]
            else:
                jj.random=None
            hh=hh.next
            
            jj=jj.next    
            # head=head.next
        kkk=kk
        # while(kk):
        #     print(kk.val)
        #     kk=kk.next
            
        return kkk
#             if g[ind]!=-1:
#                 g[ind].random=res
                
#             res=Node(head.val)
#             if kmk==1:
#                 ll=res
#             kmk=0
#             mm=head.next
#             if mm:
                
#                 jk=Node(mm.val)
#                 res.next=jk
#             if head.random !=None:
#                 g[head.random]=res
#             else:
#                 res.random=None
#             head=head.next
#             ind+=1
#             # res=res.next
            
#         return ll
            
            # if k==0:
            #     res.val=head.val
            #     mm=head.next
                
                
                
            
            
class Solution {
    public Node copyRandomList(Node head) {
        
        if(head == null)
            return null;
        
        HashMap<Node,Node> map = new HashMap<>();
        
        //node to be returned
        Node ans = new Node(head.val);
        
        //temproary pointer to the ans node
        Node tempAns = ans;
        
        Node temp = head;
        
        map.put(head,ans);
        temp = temp.next;
        
        
        //loop to store the lookalike new nodes of the original ones
        //create the new list side by side
        while(temp != null){
            Node x = new Node(temp.val);
            map.put(temp,x);
            tempAns.next = x;
            tempAns = x;
            temp = temp.next;
        }
        
        //repointing them to the start
        temp = head;
        tempAns = ans;
        
        //will have lookup of O(1) for the random nodes;
        while(temp!=null){
            tempAns.random = map.get(temp.random);
            tempAns = tempAns.next;
            temp = temp.next;
        }
        
        return ans;
        
    }
}
class Solution {
public:
    Node* copyRandomList(Node* head) {

        if(head==NULL){
            return NULL;
        }

        Node* temp= head;
        while(temp!=NULL){
            Node* m= new Node(temp->val);
            m->next= temp->next;
            temp->next= m;
            temp = temp->next->next;
        }
        temp= head;
        while(temp!=NULL){
            if(temp->random==NULL){
                temp->next->random=NULL;
            }
            else{
                temp->next->random= temp->random->next;
            }

            temp= temp->next->next;
        }
        temp= head;
        Node* p= new Node(-1);
        Node* r= p;
        Node* q= head;
        while(q!=NULL){
            q= q->next->next;
            p->next=temp->next;
            p=p->next;
            temp->next=q;
            temp= temp->next;
        }
        p->next=NULL;

        return r->next;
    }
};
var copyRandomList = function(head) {
    if(!head) return head
    let curr = head;

    // manipulate the Linked List and store a copy node at every nod's next
    while(curr != null){
        let next = curr.next;
        curr.next = new Node(curr.val);
        curr.next.next = next
        curr = next
    }
    // console.log(head)

    // Setting random point of copied node according to original node
    curr = head;
    while(curr != null && curr.next != null){
        if(curr.random){
            curr.next.random = curr.random.next
        }else{
            curr.next.random = null
        }
        curr = curr.next.next
    }
    // console.log(head.next.next.next)

    // break the like made for linking copy node with original node
    curr = head;
    let ans = head.next;

    while(curr != null){
        let temp = curr.next;
        if(temp){
        curr.next = temp.next;
        curr = curr.next;
        }
        if(curr){
        temp.next = curr.next;
        temp = temp.next
        }
    }
    return ans
};
Copy List with Random Pointer