Most of my time consumed in understanding the question,but once understood its 10 minute task


#1

/**

  • Definition for singly-linked list.

  • struct RandomListNode {

  • int label;
    
  • RandomListNode *next, *random;
    
  • RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
    
  • };
    /
    unordered_map<RandomListNode
    ,RandomListNode*> m;
    RandomListNode* getnode(RandomListNode* head){
    RandomListNode* t=new RandomListNode(head->label);
    if(m.find(t)==m.end()){
    // m[head]=t;
    return t;
    }
    else
    getnode(head);
    }
    RandomListNode* Solution::copyRandomList(RandomListNode* a) {
    RandomListNode* head=a;
    m.clear();
    while(head!=NULL){
    RandomListNode* temp=getnode(head);
    m[head]=temp;
    head=head->next;
    }
    head=a;
    RandomListNode* ans;
    ans=m[head];

    while(head!=NULL){
    ans->next=m[head->next];
    ans->random=m[head->random];
    ans=ans->next;
    head=head->next;
    }
    return m[a];
    }