2 Star 10 Fork 2

国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_138.java 2.29 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
package com.hit.basmath.learn.linked_list;
import java.util.HashMap;
import java.util.Map;
/**
* 138. Copy List with Random Pointer
* <p>
* 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.
* <p>
* Return a deep copy of the list.
* <p>
* Example 1:
* <p>
* Input:
* <p>
* {"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1}
* <p>
* Explanation:
* <p>
* 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.
* <p>
* Note:
* <p>
* You must return the copy of the given head as a reference to the cloned list.
*/
public class _138 {
// HashMap which holds old nodes as keys and new nodes as its values.
private Map<Node, Node> visitedHash = new HashMap<>();
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
// If we have already processed the current node, then we simply return the cloned version of
// it.
if (this.visitedHash.containsKey(head)) {
return this.visitedHash.get(head);
}
// Create a new node with the value same as old node. (i.e. copy the node)
Node node = new Node(head.val, null, null);
// Save this value in the hash map. This is needed since there might be
// loops during traversal due to randomness of random pointers and this would help us avoid
// them.
this.visitedHash.put(head, node);
// Recursively copy the remaining linked list starting once from the next pointer and then from
// the random pointer.
// Thus we have two independent recursive calls.
// Finally we update the next and random pointers for the new node created.
node.next = this.copyRandomList(head.next);
node.random = this.copyRandomList(head.random);
return node;
}
class Node {
public int val;
public Node next;
public Node random;
public Node() {
}
public Node(int _val, Node _next, Node _random) {
val = _val;
next = _next;
random = _random;
}
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助