【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;
    }
};
复制代码

 

posted on   承续缘  阅读(240)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示