C++ | Copy List | recursive + unordered_map


#1
RandomListNode* deepCopy( \
                            RandomListNode* A, \
                            unordered_map<RandomListNode *, RandomListNode *> &umap) {
    if (not A)   return nullptr;
    if (umap.find(A) == umap.end()) {
        RandomListNode *temp = new RandomListNode (A->label);
        umap.insert({A, temp});
        temp->next = deepCopy (A->next, umap);
        temp->random = deepCopy (A->random, umap);
    }
    return umap[A];
}
RandomListNode* Solution::copyRandomList(RandomListNode* A) {
    unordered_map<RandomListNode *, RandomListNode *> umap;
    return deepCopy(A, umap);
}