[Leetcode]138. Copy List with Random Pointer

本文介绍两种深拷贝含随机指针的单链表方法。方法一通过在原节点间插入克隆节点并调整指针完成;方法二使用哈希映射记录节点位置,创建新链表并根据映射设置随机指针。

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.

解法1:

/**
 * 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 == nullptr) return head;
        
        RandomListNode *p = head;
        while (p != nullptr) {
            RandomListNode *pCloned = new RandomListNode(p->label);
            pCloned->next = p->next;
            p->next = pCloned;
            p = pCloned->next;
        }
        
        p = head;
        while (p != nullptr) {
            if (p->random != nullptr)
                p->next->random = p->random->next;
            p = p->next->next;
        }
        
        p = head;
        RandomListNode *pCloneHead = head->next, *pCloned = head->next;
        while (p != nullptr) {
            p->next = p->next->next;
            if (pCloned->next != nullptr)
                pCloned->next = pCloned->next->next;
            p = p->next;
            pCloned = pCloned->next;
        }
        return pCloneHead;
    }
};
解法2:

/**
 * 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) {
        RandomListNode *p = nullptr, *pCloned = nullptr, *pClonedHead = nullptr;
        map<RandomListNode*, int> mp;
        int pos = 0;
        for (p = head; p != nullptr; p = p->next) {
            mp[p] = pos++;
        }
        
        vector<RandomListNode*> vec;
        for (p = head; p != nullptr; p = p->next) {
            RandomListNode *node = new RandomListNode(p->label);
            vec.push_back(node);
            if (pClonedHead == nullptr) {
                pClonedHead = node;
                pCloned = node;
            }
            else {
                pCloned->next = node;
                pCloned = node;
            }
        }
        
        for (pCloned = pClonedHead, p = head; pCloned != nullptr && p != nullptr; p = p->next, pCloned = pCloned->next) {
            if (p->random != nullptr) {
                pos = mp[p->random];
                pCloned->random = vec[pos];
            }
        }
        return pClonedHead;
    }
};

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值