题解 | #复杂链表的复制#

复杂链表的复制

https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};
*/
#include <unordered_map>
class Solution {
public:
    RandomListNode* Clone(RandomListNode* pHead) {
        if(pHead==nullptr){
            return nullptr;
        }
        unordered_map<RandomListNode*, RandomListNode*>  map;//创建哈希表,将原来的链表每个点都copy一份。
        RandomListNode* cur=pHead;
        while(cur!=nullptr){
            map[cur]=new RandomListNode(cur->label);
            cur=cur->next;
        } 
        cur=pHead;
        while(cur!=nullptr){
            map[cur]->next=map[cur->next];
            map[cur]->random=map[cur->random];
            cur=cur->next;
        }
        return map[pHead];
    }
};

全部评论

相关推荐

昨天 18:22
门头沟学院 运营
是正式编吗?稳定吗?工资待遇怎么样?转正可以转到腾讯总部吗?
Java抽象带篮子:不是,不稳定,一般,不行
投递腾讯云智研发等公司8个岗位 >
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务