We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ef1612e commit 079bb88Copy full SHA for 079bb88
solution/0133.Clone Graph/Solution.java
@@ -0,0 +1,15 @@
1
+class Solution {
2
+ private Map<Node, Node> cache;
3
+ public Node cloneGraph(Node node) {
4
+ cache = new HashMap<>(16);
5
+ return helper(node);
6
+ }
7
+ private Node helper(Node node) {
8
+ if (node == null) return null;
9
+ else if (cache.containsKey(node)) return cache.get(node);
10
+ Node nodeCopy = new Node(node.val,new ArrayList<>());
11
+ cache.put(node, nodeCopy);
12
+ for (Node neighbor : node.neighbors) nodeCopy.neighbors.add(helper(neighbor));
13
+ return nodeCopy;
14
15
+}
0 commit comments