如何复制一个含有随机指针节点的链表
生活随笔
收集整理的這篇文章主要介紹了
如何复制一个含有随机指针节点的链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
復制含有隨機指針節點的鏈表
Node 類中的 value 是節點值,next 指針和正常單鏈表中 next 指針的意義一樣,都指向下一個節點,rand 指針是 Node
類中新增的指針,這個指針可能指向鏈表中的任意一個節點,也可能指向 null。
給定一個由 Node 節點類型組成的無環單鏈表的頭節點 head,請實現一個函數完成這個鏈表中所有結構的復制,并返
回復制的新鏈表的頭節點。例如:鏈表 1->2->3->null,假設 1 的 rand 指針指向 3,2 的 rand 指針指向 null,3
的 rand 指針指向 1。復制后的鏈表應該也是這種結構,比如,1->2->3->null,1 的 rand 指針指向 3,2 的 rand
指針指向 null,3 的 rand 指針指向 1,最后返回 1。
時間復雜度為 O(N),額外空間復雜度 O(1)
思路:兩種實現辦法,一種是使用額外空間,建立一個哈希表,用于存儲原鏈表節點與對應的新建鏈表節點的對應,方法是首先遍歷一遍原鏈表,可以將新建鏈表每個節點的next指針按照原鏈表的連接順序連接,即哈希表中的對應關系是:1->1',2->2',3->3',1'.next->2'.next->3'.next->null,再遍歷第二次原鏈表,根據原鏈表的rand指針和哈希表中原鏈表與新鏈表每個節點的對應關系,將新鏈表中每個節點的rand指針按照原鏈表中每個節點的rand指向順序連接,即可實現,即1'.rand->3'.rand,2'.rand->null,3'.rand->1'。第二種辦法,將原鏈表的每個節點新建并用原鏈表中每個節點的next指針連接,即1->1'->2->2'->3->3'->null,之后遍歷原鏈表的每個節點的rand指針,即可利用原鏈表的每個節點的next指向新鏈表的拷貝節點,對新鏈表的每個節點的rand指針進行賦值,即可實現。
public static class Node{public int value;public Node next;public Node rand;public Node(int data){this.value = data;} }public static Node copyListWithRand_1(Node head){HashMap<Node, Node> map = new HashMap<Node,Node>();Node cur = head;while(cur != null){map.put(cur, new Node(cur.value));cur = cur.next;}cur = head;while(cur != null){map.get(cur).next = map.get(cur.next);map.get(cur).rand = map.get(cur.rand);cur = cur.next;}return map.get(head); } public static Node copyListWithRand_2(Node head){if(head == null){return null;}Node cur = head;Node next = null;while(cur != null){next = cur.next;cur.next = new Node(cur.value);cur.next.next = next;cur = next;}cur = head;Node curCopy = null;while(cur != null){next = cur.next.next;curCopy = cur.next;curCopy.rand = cur.rand != null ? cur.rand.next : null;cur = next;}Node res = head.next;cur = head;while(cur != null){next = cur.next.next;curCopy = cur.next;cur.next = next;curCopy.next = next != null ? next.next : null;cur = next;}return res; }
一種特殊的鏈表節點類描述如下:
public class Node {public int value;public Node next;public Node rand;public Node(int data) {this.value = data;} }Node 類中的 value 是節點值,next 指針和正常單鏈表中 next 指針的意義一樣,都指向下一個節點,rand 指針是 Node
類中新增的指針,這個指針可能指向鏈表中的任意一個節點,也可能指向 null。
給定一個由 Node 節點類型組成的無環單鏈表的頭節點 head,請實現一個函數完成這個鏈表中所有結構的復制,并返
回復制的新鏈表的頭節點。例如:鏈表 1->2->3->null,假設 1 的 rand 指針指向 3,2 的 rand 指針指向 null,3
的 rand 指針指向 1。復制后的鏈表應該也是這種結構,比如,1->2->3->null,1 的 rand 指針指向 3,2 的 rand
指針指向 null,3 的 rand 指針指向 1,最后返回 1。
時間復雜度為 O(N),額外空間復雜度 O(1)
思路:兩種實現辦法,一種是使用額外空間,建立一個哈希表,用于存儲原鏈表節點與對應的新建鏈表節點的對應,方法是首先遍歷一遍原鏈表,可以將新建鏈表每個節點的next指針按照原鏈表的連接順序連接,即哈希表中的對應關系是:1->1',2->2',3->3',1'.next->2'.next->3'.next->null,再遍歷第二次原鏈表,根據原鏈表的rand指針和哈希表中原鏈表與新鏈表每個節點的對應關系,將新鏈表中每個節點的rand指針按照原鏈表中每個節點的rand指向順序連接,即可實現,即1'.rand->3'.rand,2'.rand->null,3'.rand->1'。第二種辦法,將原鏈表的每個節點新建并用原鏈表中每個節點的next指針連接,即1->1'->2->2'->3->3'->null,之后遍歷原鏈表的每個節點的rand指針,即可利用原鏈表的每個節點的next指向新鏈表的拷貝節點,對新鏈表的每個節點的rand指針進行賦值,即可實現。
public static class Node{public int value;public Node next;public Node rand;public Node(int data){this.value = data;} }public static Node copyListWithRand_1(Node head){HashMap<Node, Node> map = new HashMap<Node,Node>();Node cur = head;while(cur != null){map.put(cur, new Node(cur.value));cur = cur.next;}cur = head;while(cur != null){map.get(cur).next = map.get(cur.next);map.get(cur).rand = map.get(cur.rand);cur = cur.next;}return map.get(head); } public static Node copyListWithRand_2(Node head){if(head == null){return null;}Node cur = head;Node next = null;while(cur != null){next = cur.next;cur.next = new Node(cur.value);cur.next.next = next;cur = next;}cur = head;Node curCopy = null;while(cur != null){next = cur.next.next;curCopy = cur.next;curCopy.rand = cur.rand != null ? cur.rand.next : null;cur = next;}Node res = head.next;cur = head;while(cur != null){next = cur.next.next;curCopy = cur.next;cur.next = next;curCopy.next = next != null ? next.next : null;cur = next;}return res; }
總結
以上是生活随笔為你收集整理的如何复制一个含有随机指针节点的链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 构造数组MaxTree、环形单链表的约瑟
- 下一篇: 两个单链表相交的一系列问题