zoukankan      html  css  js  c++  java
  • 【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;
        }
    };
  • 相关阅读:
    泛型接口(C# 编程指南) From MSDN
    不知道是不是心理作用,我怎么觉得在Fedora下写cnblogs比Windows下快。
    VS.NET 2005真是太好用了!
    写了个打字游戏,可是有问题(C#)
    C#多线程测试
    关于继承的一个小程序
    VS.NET 2008 试用
    基本排序算法及分析(二):冒泡排序
    基本排序算法及分析(三):shell排序
    [导入]一维数组输出杨辉三角形
  • 原文地址:https://www.cnblogs.com/xbf9xbf/p/4469072.html
Copyright © 2011-2022 走看看