【Copy List with Random Pointer】cpp

题目

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

代码

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
            if ( !head ) return head;
            std::map<RandomListNode *, RandomListNode *> oriNext;
            RandomListNode *pOri = head;
            RandomListNode dummy(-1);
            RandomListNode *pCopy = &dummy;
            // first round visit
            while (pOri)
            {
                // store the original Node's next pointer
                oriNext[pOri] = pOri->next;
                // create copy Node
                pOri->next = new RandomListNode(pOri->label);
                pCopy->next = pOri->next;
                pCopy->next->random = pOri;
                pCopy = pCopy->next;
                // move to the next Node
                pOri = oriNext[pOri];
            }
            // second round visit
            pCopy = dummy.next;
            while (pCopy)
            {
                pCopy->random = pCopy->random->random ? pCopy->random->random->next : NULL;
                pCopy = pCopy->next;
            }
            // third round recover original list
            for (std::map<RandomListNode *, RandomListNode *>::iterator i = oriNext.begin(); i != oriNext.end(); ++i)
            {
                i->first->next = i->second;
            }
            return dummy.next;
    }
};

Tips

典型的链表深拷贝,之前用python做的时候用了O(1)空间技巧(http://www.cnblogs.com/xbf9xbf/p/4216655.html)。

这次尝试了用hashmap的O(n)空间的技巧,原因是感觉hashmap可能更通用一些。

思路主要参考了下面这个日志:http://www.geeksforgeeks.org/a-linked-list-with-next-and-arbit-pointer/

需要注意的是:如果原来的链表节点random为空,就不要再往下寻找next;需要加一个保护判断

===============================================================

第二次过这道题,hashmap的大体思路画画图还记得,代码调试了几次才AC。

(1)不要忘记恢复原来的链表

(2)在拷贝random的时候,不要忘记copy = copy->next这条语句

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
            if (!head) return NULL;
            map<RandomListNode*, RandomListNode*> ori_next;
            RandomListNode* p = head;
            RandomListNode dummpy(-1);
            RandomListNode* copy = &dummpy;
            while (p)
            {
                copy->next = new RandomListNode(p->label);
                copy->next->random = p;
                ori_next[p] = p->next;
                p = p->next;
                copy->next->random->next = copy->next;
                copy = copy->next;
            }
            copy = dummpy.next;
            while (copy)
            {
                copy->random = copy->random->random ? copy->random->random->next : NULL;
                copy = copy->next;
            }
            for ( map<RandomListNode*, RandomListNode*>::iterator i=ori_next.begin(); i!=ori_next.end(); ++i )
            {
                i->first->next = ori_next[i->first];
            }
            return dummpy.next;
    }
};
原文地址:https://www.cnblogs.com/xbf9xbf/p/4469072.html