LeetCode:138 复制带随机指针的链表

122 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第14天,点击查看活动详情

题目

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

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

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

返回复制链表的头节点。

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

  • val:一个表示 Node.val 的整数。
  • random_index:随机指针指向的节点索引(范围从 0n-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 或指向链表中的节点。

解题

解题一:HashMap + 递归

思路

  1. 使用 Map 存放 <旧的 Node,新的 Node>
  2. 使用递归的方法将旧的 Node 替换为新的 Node

代码

// 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 {
    static HashMap<Node, Node> map = new HashMap<>();

    public Node copyRandomList(Node head) {
        // 复制操作
        if (head == null) {
            return null;
        }
        if (map.get(head) == null) {
            Node node = new Node(head.val);
            map.put(head, node);
            node.next = copyRandomList(head.next);
            node.random = copyRandomList(head.random);
        }
        return map.get(head);
    }
}

总结

性能分析

  • 执行耗时:0 ms,击败了 100.00% 的 Java 用户
  • 内存消耗:41.1 MB,击败了 26.63% 的 Java 用户

题解二:节点拆分

思路

  1. 在原本的链表中的每个节点后面复制一个新节点
  2. 将随机节点改为当前随机节点的下一个节点
  3. 删除原有的节点

代码

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;
        }
        // 1. 复制节点
        Node tempIndex;
        for (tempIndex = head; tempIndex != null ; tempIndex = tempIndex.next.next) {
            Node copyNode = new Node(tempIndex.val);
            copyNode.next = tempIndex.next;
            tempIndex.next = copyNode;
        }
        // 2. 转移随机节点
        for (tempIndex = head; tempIndex != null; tempIndex = tempIndex.next.next) {
            tempIndex.next.random = tempIndex.random == null ? null : tempIndex.random.next;
        }
        // 3. 删除原有节点
        Node result = head.next;
        for (tempIndex = head; tempIndex != null; tempIndex = tempIndex.next) {
            Node tempNext = tempIndex.next.next;
            if (tempNext != null) {
                // 复制后的链表
                tempIndex.next.next = tempNext.next;
            }
            // 原链表,保证原链表无变化,否则 Next pointer of node with label 7 from the original list was modified
            tempIndex.next = tempNext;
        }
        return result;
    }
}

总结

注意:不能改变原有的链表

性能分析

  • 执行耗时:0 ms,击败了 100.00% 的 Java 用户
  • 内存消耗:40.9 MB,击败了 65.03% 的 Java 用户