【每日算法】复制带随机指针的链表:「哈希表」&「原地算法」|Python 主题月

本文正在参加「Python主题月」,详情查看 活动连接html

题目描述

这是 LeetCode 上的 138. 复制带随机指针的链表 ,难度为 中等node

Tag : 「哈希表」、「链表」git

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

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

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

返回复制链表的头节点。app

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

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

你的代码 只 接受原链表的头节点 head 做为传入参数。oop

 

示例 1: post

输入: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]]
复制代码

示例 4:

输入:head = []

输出:[]

解释:给定的链表为空(空指针),所以返回 null。
复制代码

提示:

  • 0 <= n <= 1000
  • -10000 <= Node.val <= 10000

模拟 + 哈希表

若是不考虑 random 指针的话,对一条链表进行拷贝,咱们只须要使用两个指针:一个用于遍历原链表,一个用于构造新链表(始终指向新链表的尾部)便可。这一步操做可看作是「建立节点 + 构建 next 指针关系」。

如今在此基础上增长一个 random 指针,咱们能够将 next 指针和 random 指针关系的构建拆开进行:

  1. 先不考虑 random 指针,和本来的链表复制同样,建立新新节点,并构造 next 指针关系,同时使用「哈希表」记录原节点和新节点的映射关系;
  2. 对原链表和新链表进行同时遍历,对于原链表的每一个节点上的 random 都经过「哈希表」找到对应的新 random 节点,并在新链表上构造 random 关系。

Java 代码:

class Solution {
    public Node copyRandomList(Node head) {
        Map<Node, Node> map = new HashMap<>();
        Node dummy = new Node(-1);
        Node tail = dummy, tmp = head;
        while (tmp != null) {
            Node node = new Node(tmp.val);
            map.put(tmp, node);
            tail.next = node;
            tail = tail.next;
            tmp = tmp.next;
        }
        tail = dummy.next;
        while (head != null) {
            if (head.random != null) tail.random = map.get(head.random);
            tail = tail.next;
            head = head.next;
        }
        return dummy.next;
    }
}
复制代码

Python 3 代码:

class Solution:
    def copyRandomList(self, head: 'Node') -> 'Node':
        hashmap = dict()
        dummy = Node(-1)
        tail, tmp = dummy, head
        while tmp:
            node = Node(tmp.val)
            hashmap[tmp] = node
            tail.next = node
            tail = tail.next
            tmp = tmp.next
        tail = dummy.next
        while head:
            if head.random:
                tail.random = hashmap[head.random]
            tail = tail.next
            head = head.next
        return dummy.next
复制代码
  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( n ) O(n)

模拟(原地算法)

显然时间复杂度上没法优化,考虑如何下降空间(不使用「哈希表」)。

咱们使用「哈希表」的目的为了实现原节点和新节点的映射关系,更进一步的是为了快速找到某个节点 random 在新链表的位置。

那么咱们能够利用原链表的 next 作一个临时中转,从而实现映射。

具体的,咱们能够按照以下流程进行:

  1. 对原链表的每一个节点节点进行复制,并追加到原节点的后面;
  2. 完成 1 1 操做以后,链表的奇数位置表明了原链表节点,链表的偶数位置表明了新链表节点,且每一个原节点的 next 指针执行了对应的新节点。这时候,咱们须要构造新链表的 random 指针关系,能够利用 link[i + 1].random = link[i].random.next i i 为奇数下标,含义为 新链表节点的 random 指针指向旧链表对应节点的 random 指针的下一个值
  3. 对链表进行拆分操做。

image.png

Java 代码:

class Solution {
    public Node copyRandomList(Node head) {
        if (head == null) return null;
        Node dummy = new Node(-1);
        dummy.next = head;
        while (head != null) {
            Node node = new Node(head.val);
            node.next = head.next;
            head.next = node;
            head = node.next;
        }
        head = dummy.next;
        while (head != null) {
            if (head.random != null) {
                head.next.random = head.random.next;
            }
            head = head.next.next;
        }
        head = dummy.next;
        Node ans = head.next;
        while (head != null) {
            Node tmp = head.next;
            if (head.next != null) head.next = head.next.next;
            head = tmp;
        }
        return ans;
    }
}
复制代码

Python 3 代码:

class Solution:
    def copyRandomList(self, head: 'Node') -> 'Node':
        if not head:
            return None
        dummy = Node(-1)
        dummy.next = head
        while head:
            node = Node(head.val)
            node.next = head.next
            head.next = node
            head = node.next
        head = dummy.next
        while head:
            if head.random:
                head.next.random = head.random.next
            head = head.next.next
        head = dummy.next
        ans = head.next
        while head:
            tmp = head.next
            if head.next:
                head.next = head.next.next
            head = tmp
        return ans
复制代码
  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( 1 ) O(1)

最后

这是咱们「刷穿 LeetCode」系列文章的第 No.138 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,咱们将先将全部不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路之外,还会尽量给出最为简洁的代码。若是涉及通解还会相应的代码模板。

为了方便各位同窗可以电脑上进行调试和提交代码,我创建了相关的仓库:github.com/SharingSour…

在仓库地址里,你能够看到系列文章的题解连接、系列文章的相应代码、LeetCode 原题连接和其余优选题解。

相关文章
相关标签/搜索