Why my solution is giving wrong answer?


#1

RandomListNode* Solution::copyRandomList(RandomListNode* A)
{
// RandomListNode* head1 = A;
unordered_map<int,RandomListNode*> a1;
unordered_map<RandomListNode*,RandomListNode*> a2;

RandomListNode* temp = new RandomListNode(0);
RandomListNode* head = temp;
while(A != NULL)
{   
    int a = A->label;
    RandomListNode* new_node = new RandomListNode(a);
    temp->next = new_node;
    temp= temp->next;
    a1[a] = A->random;
    a2[A] = temp;
    A = A->next;
}
temp = head;
temp = temp->next;
while(temp!=NULL)
{   
    int a_1 = temp->label;
    RandomListNode* d = a1[a_1];
    RandomListNode* d1 = a2[d];
    temp->random = d1;
    temp= temp->next;
}

return head->next;

}