深拷贝 138. 复制带随机指针的链表 ( 类似克隆图133)

25 篇文章 0 订阅

138. 复制带随机指针的链表

给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。

要求返回这个链表的 深拷贝。

我们用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:

val:一个表示 Node.val 的整数。
random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。

DFS+递归实现深拷贝

/*
// 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;
        if(visited[head]) return visited[head];
        Node * res=new Node(head->val);
        visited[head]=res;
        res->next=copyRandomList(head->next);
        res->random=copyRandomList(head->random);
        return res;
    }
private:
    unordered_map<Node*,Node*> visited;
};

BFS+队列实现深拷贝

class Solution {
public:
    Node* copyRandomList(Node* head) {
        if(!head) return head;
        visited[head]=new Node(head->val);
        queue<Node*> Q;
        Q.push(head);
        while(!Q.empty()){
            Node* tmp=Q.front();
            Q.pop();
            if(tmp->next){
                if(!visited[tmp->next]) {
                    visited[tmp->next]=new Node(tmp->next->val);
                    Q.push(tmp->next);
                }
                visited[tmp]->next=visited[tmp->next];
            }
            if(tmp->random){
                if(!visited[tmp->random]){
                    visited[tmp->random]=new Node(tmp->random->val);
                    Q.push(tmp->random);
                }
                visited[tmp]->random=visited[tmp->random];
            }
        }
        return visited[head];
    }
private:
    unordered_map<Node*,Node*> visited;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用哈希表来实现对随机指针链表进行复刻。具体步骤如下: 1. 遍历原链表,将每个节点都复制一份,并将复制的节点插入到原节点的后面,形成一个新的链表。 2. 再次遍历原链表,将每个新节点的随机指针指向原节点随机指针的下一个节点。 3. 将新链表拆分成两个链表,一个是原链表复制,一个是新链表复制。 下面是Python代码实现: ```python class Node: def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): self.val = int(x) self.next = next self.random = random class Solution: def copyRandomList(self, head: 'Node') -> 'Node': if not head: return None # 第一次遍历,复制节点并插入到原节点后面 cur = head while cur: node = Node(cur.val, cur.next) cur.next = node cur = node.next # 第二次遍历,处理随机指针 cur = head while cur: if cur.random: cur.next.random = cur.random.next cur = cur.next.next # 第三次遍历,拆分链表 cur = head new_head = head.next while cur.next: tmp = cur.next cur.next = tmp.next cur = tmp return new_head ``` 注释: 1. 定义一个Node类,表示链表节点,包含三个属性:值val、下一个节点next、随机指针random。 2. 定义一个Solution类,包含一个方法copyRandomList,用于复刻随机指针链表。 3. 第一次遍历原链表复制每个节点并插入到原节点的后面,形成一个新的链表。 4. 第二次遍历原链表,处理每个新节点的随机指针,使其指向原节点随机指针的下一个节点。 5. 第三次遍历原链表,拆分新链表和原链表复制。 6. 返回新链表的头节点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值