C++ Recursive solution, the same as copy graph


  • 0
    K
    unordered_map<RandomListNode*, RandomListNode*>mp;
    RandomListNode *copyRandomList(RandomListNode *head) {
    	if (head == NULL)return head;
    	if (mp.find(head) == mp.end())
    	{
    		mp[head] = new RandomListNode(head->label);
    		mp[head]->next = copyRandomList(head->next);
    		mp[head]->random = copyRandomList(head->random);
    	}
    	return mp[head];
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.