力扣 138. 复制带随机指针的链表 链表+哈希

38 篇文章 0 订阅
32 篇文章 1 订阅

https://leetcode-cn.com/problems/copy-list-with-random-pointer/
在这里插入图片描述

思路一:首先忽略 r a n d o m random random指针,遍历给定链表 h 1 h_1 h1,同时建立出它的深拷贝 h 2 h_2 h2,并建立从 h 1 h_1 h1 h 2 h_2 h2的映射。然后我们再次遍历链表 h 1 h_1 h1,有了映射关系,就可以修改 r a n d o m random random指针了。

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(!head)
            return head;
        unordered_map<Node*,Node*> m;
        Node myHead(0);
        Node *cur1=&myHead,*cur2=head;
        while(cur2){
            cur1->next=new Node(cur2->val);
            cur1=cur1->next;
            m[cur2]=cur1;
            cur2=cur2->next;
        }
        cur1=myHead.next;
        cur2=head;
        while(cur2){
            cur1->random=m[cur2->random];
            cur1=cur1->next;
            cur2=cur2->next;
        }
        return myHead.next;
    }
};

思路二:和思路一一样,但是我们可以一步到位,无需遍历 h 1 h_1 h1两遍。

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(!head)
            return head;
        unordered_map<Node*,Node*> m;
        Node myHead(0);
        Node *cur1=&myHead,*cur2=head;
        while(cur2){
            if(m.find(cur2)!=m.end())
                cur1->next=m[cur2];
            else{
                cur1->next=new Node(cur2->val);
                m[cur2]=cur1->next;
            }
            cur1=cur1->next;
            if(!cur2->random){
                cur2=cur2->next;
                continue;
            }
            //处理random指针
            if(m.find(cur2->random)!=m.end())
                cur1->random=m[cur2->random];
            else{
                cur1->random=new Node(cur2->random->val);
                m[cur2->random]=cur1->random;
            }
            cur2=cur2->next;
        }
        return myHead.next;
    }
};

思路三:这个思路相当巧妙,可以做到 O ( 1 ) O(1) O(1)的空间复杂度。先遍历一遍 h 1 h_1 h1,每个节点进行自我复制,并将其连接到自己后面,比如: A − > B − > C A->B->C A>B>C,经过这次操作后就变成了 A − > A − > B − > B − > C − > C A->A->B->B->C->C A>A>B>B>C>C;再遍历一遍 h 1 h_1 h1,这个时候处理 r a n d o m random random指针就比较容易了;最后遍历一遍 h 1 h_1 h1,将其拆分成两个链表即可。

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(!head)
            return head;
        Node *cur=head,*nxt,*tmp;
        //复制自身并连接在自己后面
        while(cur){
            nxt=cur->next;
            cur->next=new Node(cur->val);
            cur->next->next=nxt;
            cur=nxt;
        }
        cur=head;
        //处理random指针
        while(cur){
            nxt=cur->next;
            tmp=nxt->next;
            if(cur->random)
                nxt->random=cur->random->next;
            cur=tmp;
        }
        Node ans(0),*pre=&ans;
        cur=head;
        while(cur){
            nxt=cur->next;
            tmp=nxt->next;
            cur->next=tmp;
            cur=tmp;
            pre->next=nxt;
            pre=nxt;
        }
        return ans.next;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值