【每日算法】複製帶隨機指針的鏈表:「哈希表」&「原地算法」|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 原題連接和其餘優選題解。

相關文章
相關標籤/搜索