138. Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
138. Copy List with Random Pointer
文章图片

Example 1:Input: {"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}

Explanation:
Node 1’s value is 1, both of its next and random pointer points to Node 2.
Node 2’s value is 2, its next pointer points to null and its random pointer points to itself.
遍历第一遍,复制node,形成新链表。同时,将老的node与新的node一一对应,放入map
遍历第二遍,赋值random。在map找到对应的节点,赋值到新链表中的node里。
class Solution { public Node copyRandomList(Node head) { if (head == null) { return null; } Map map = new HashMap<>(); Node cur = head; Node dummy = new Node(); Node pre = dummy; while (cur != null) { Node node = new Node(); node.val = cur.val; map.put(cur, node); pre.next = node; pre = node; cur = cur.next; }Node curNew = dummy.next; Node curOld = head; while (curOld != null) { Node random = map.get(curOld.random); curNew.random = random; curNew = curNew.next; curOld = curOld.next; } return dummy.next; } }

第一遍遍历生成所有新节点时同时建立一个原节点和新节点的 HashMap,第二遍给随机指针赋值时,查找时间是常数级。
当然,如果使用 HashMap 占用额外的空间,如果这道题限制了空间的话,就要考虑别的方法。下面这个方法很巧妙,可以分为以下三个步骤:
  1. 在原链表的每个节点后面拷贝出一个新的节点。
  2. 依次给新的节点的随机指针赋值,而且这个赋值非常容易 cur->next->random = cur->random->next。
  3. 断开链表可得到深度拷贝后的新链表。
举个例子来说吧,比如原链表是 1(2) -> 2(3) -> 3(1),括号中是其 random 指针指向的结点,那么这个解法是首先比遍历一遍原链表,在每个结点后拷贝一个同样的结点,但是拷贝结点的 random 指针仍为空,则原链表变为 1(2) -> 1(null) -> 2(3) -> 2(null) -> 3(1) -> 3(null)。然后第二次遍历,是将拷贝结点的 random 指针赋上正确的值,则原链表变为 1(2) -> 1(2) -> 2(3) -> 2(3) -> 3(1) -> 3(1),注意赋值语句为:
cur->next->random = cur->random->next;
【138. Copy List with Random Pointer】这里的 cur 是原链表中结点,cur->next 则为拷贝链表的结点,cur->next->random 则为拷贝链表的 random 指针。cur->random 为原链表结点的 random 指针指向的结点,因为其指向的还是原链表的结点,所以我们要再加个 next,才能指向拷贝链表的结点。最后再遍历一次,就是要把原链表和拷贝链表断开即可

    推荐阅读