# Getting MLE using hasmap approach

#1
``````RandomListNode* Solution::copyRandomList(RandomListNode* A) {

unordered_map<RandomListNode*,RandomListNode* >m;
int i=1;

while(A!=NULL)
{

if(m.find(A)==m.end())
{RandomListNode *a = new RandomListNode(A->label);
m.insert(make_pair(A,a));}
i++;
A=A->next;
}
while(A!=NULL)
{
m[A]->label= A->label;
if(A->next!=NULL)
m[A]->next = m[A->next];
if(A->random!=NULL)
m[A]->random = m[A->random];
A = A->next;
}

}``````

#2

My solution works after using two hashmaps. I have used key as index of the node which is less costly than using node as a key

``````    /**
``````
• Definition for singly-linked list.

• struct RandomListNode {

• ``````int label;
``````
• ``````RandomListNode *next, *random;
``````
• ``````RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
``````
• };
/
RandomListNode

`````` RandomListNode* ptr = head, *l1 = NULL, *temp = NULL;
unordered_map<int, RandomListNode*>copy_map;
int index = 0;

while(ptr != NULL) {
if(l1 == NULL) {
l1 = new RandomListNode(ptr -> label);
temp = l1;
} else {
temp -> next = new RandomListNode(ptr -> label);
temp = temp -> next;
}

copy_map[index] = temp;
index++;
ptr = ptr -> next;
}

while(ptr != NULL) {
int index1 = head_map[ptr];
int index2 = -1;