【LeetCode】138. 复制带随机指针的链表(同 剑指 Offer 35)

一、题目

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

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

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

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

示例 1:

1

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

示例 2:
2

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

示例 3:

3

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

示例 4:

输入:head = []
输出:[]
解释:给定的链表为空(空指针),因此返回 null。

提示:

  • -10000 <= Node.val <= 10000
  • Node.random 为空(null)或指向链表中的节点。
  • 节点数目不超过 1000 。

二、解决

1、哈希表

思路:

利用哈希表的查询特点,构建原链表节点和新链表对应节点的键值对映射关系,再遍历构建新链表各节点的nextrandom引用指向即可。

步骤:

  1. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
    这里需要注意的是map存储键值对内容,如cur = 7,map.put(7, new(7)),前后对应不完全一样,前面是原节点,包含指针关系(7.next=3,7.random=null),后面只是一个值,指针内容为空。

  2. 构建新链表的 next 和 random 指向
    在新链表的指针关系构建中,需要利用原节点关系(key位置)。不难理解,但最好自己在纸上模拟一下,下面也举个例子补充一下细节。

代码:
// 2. 构建新链表的 next 和 random 指向
cur = head;
while(cur != null) {
	map.get(cur).next = map.get(cur.next);
	map.get(cur).random = map.get(cur.random);
	cur = cur.next;
}7.random = null,7.next=3。
map.get(cur).next = map.get(cur.next);  // (复制节点)7.next = 3(复制节点)
map.get(cur).random = map.get(cur.random);  //  (复制节点)3.random = null(复制节点)
cur = cur.next;  // cur = 3
  1. 返回新链表的头节点
    全部细节可看参考1中的幻灯片。

代码:

/*
// 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;
        Node cur = head;
        Map<Node, Node> map = new HashMap<>();
        // 1. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射
        while(cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }

        // 2. 构建新链表的 next 和 random 指向
        cur = head;
        while(cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        
        // 3. 返回新链表的头节点
        return map.get(head);
    }
}

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

2、拼接

思路:

遍历整个链表的每个节点,逐个复制并连接到原节点的后面。结构为:原节点 1 -> 新节点 1 -> 原节点 2 -> 新节点 2 -> ……,然后访问原节点random指向节点,并将这种指向关系赋给复制节点。

流程

  1. 复制各节点,构建拼接链表
    拼接后结构: n o d e 1 → n o d e 1 n e w → n o d e 2 → n o d e 2 n e w → ⋯ node1→node1_{new} →node2→node2_{new} →⋯ node1node1newnode2node2new

  2. 构建新链表各节点的random指向
    (cur的复制节点的random指针)cur.next.random = cur.random.next (cur的随机节点往后移动一位,即为新的random节点)
    1

  3. 拆分原 / 新链表
    // 拆分两链表
    pre.next = pre.next.next;
    cur.next = cur.next.next;

代码:

class Solution {
    public Node copyRandomList(Node head) {
        if(head == null) return null;
        Node cur = head;
        // 1. 复制各节点,并构建拼接链表
        while(cur != null) {
            Node tmp = new Node(cur.val);
            tmp.next = cur.next;
            cur.next = tmp;
            cur = tmp.next;
        }
        // 2. 构建各新节点的 random 指向
        cur = head;
        while(cur != null) {
            if(cur.random != null)
                cur.next.random = cur.random.next;
            cur = cur.next.next;
        }
        // 3. 拆分两链表
        cur = head.next;
        Node pre = head, res = head.next;
        while(cur.next != null) {
            pre.next = pre.next.next;
            cur.next = cur.next.next;
            pre = pre.next;
            cur = cur.next;
        }
        pre.next = null; // 单独处理原链表尾节点
        return res;      // 返回新链表头节点
    }
}

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( 1 ) O(1) O(1)

三、参考

1、剑指 Offer 35. 复杂链表的复制(哈希表 / 拼接与拆分,清晰图解)
2、图解 链表的深拷贝
3、复制带随机指针的链表
4、A solution with constant space complexity O(1) and linear time complexity O(N)
5、Java O(n) solution

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值