138-随机链表的复制

  1. 随机链表的复制
    1. 示例 1:
    2. 示例 2:
    3. 示例 3:
    4. 提示:
    5. 题解
      1. 回溯 + 哈希表
      2. 迭代 + 节点拆分

随机链表的复制

链接: https://leetcode.cn/problems/copy-list-with-random-pointer/description/

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

构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。

例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y

返回复制链表的头节点。

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

  • val:一个表示 Node.val 的整数。

  • random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为 null 。
    你的代码 只 接受原链表的头节点 head 作为传入参数。

示例 1:

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

示例 2:

输入:head = [[1,1],[2,1]]
输出:[[1,1],[2,1]]

示例 3:

输入:head = [[3,null],[3,0],[3,null]]
输出:[[3,null],[3,0],[3,null]]

提示:

  • 0 <= n <= 1000
  • -104 <= Node.val <= 104
  • Node.randomnull 或指向链表中的节点。

题解

回溯 + 哈希表

/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/
// 回溯 + 哈希表
class Solution {
    Map<Node,Node> cacheNode = new HashMap<Node,Node>();
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        // 如果当前缓存不包括head,则执行下一轮递归。避免随机链表中存在循环
        if (!cacheNode.containsKey(head)) {
            Node headNew = new Node(head.val);
            cacheNode.put(head, headNew);
            headNew.next = copyRandomList(head.next);// 递归遍历顺序链表
            headNew.random =  copyRandomList(head.random);// 递归遍历随机链表
        }
        return cacheNode.get(head);
    }
}

迭代 + 节点拆分

不用哈希表了,巧妙的利用链表的特性进行复制

  • 复制顺序链表
  • 复制随机链表
  • 拆除新的链表
/*
// Definition for a Node.
class Node {
    int val;
    Node next;
    Node random;

    public Node(int val) {
        this.val = val;
        this.next = null;
        this.random = null;
    }
}
*/
class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        // 遍历顺序链表,复制每一个节点,复制节点插入到老链表中
        for (Node node = head;node != null;node = node.next.next) {
            Node nodeNew = new Node(node.val);
            nodeNew.next = node.next;// 新的节点的下一个复制位老节点的下一个
            node.next = nodeNew;// 让老的节点的下一个指向新的节点
        }

        // 遍历顺序链表,获取复制链表节点,复制随机指针
        for (Node node = head;node!=null;node = node.next.next) {
            Node nodeNew = node.next;
            nodeNew.random = (node.random !=  null) ?node.random.next: null;
        }
        // 拆出复制的链表节点
        Node headNew = head.next;
        for (Node node = head;node != null; node = node.next) {
            Node nodeNew = node.next;
            node.next  = node.next.next;
            nodeNew.next = (nodeNew.next != null) ? nodeNew.next.next:null;
        }
        return headNew;
    }
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

💰

Title:138-随机链表的复制

Count:839

Author:攀登

Created At:2023-12-02, 14:10:24

Updated At:2024-06-15, 15:52:32

Url:http://jiafeimao-gjf.github.io/2023/12/02/138-%E9%9A%8F%E6%9C%BA%E9%93%BE%E8%A1%A8%E7%9A%84%E5%A4%8D%E5%88%B6/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

×

Help us with donation