LeetCode 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.
The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
- val: an integer representing Node.val
- random_index: the index of the node (range from 0 to n-1) where random pointer points to, or null if it does not point to any node.
idea:
it’s a deep copy related problem instead of the random related question.
class Solution {
//this hashmap use old nodes as keys and new Node(node.val) as the deep copy
HashMap<Node, Node> visited = new HashMap<Node, Node>();
private Node getCloneNode(Node node) {
if (node != null) {
if (this.visited.containsKey(node)) { //if we have this key
return this.visited.get(node); //return it's corresponding node
} else {
this.visited.put(node, new Node(node.val)); //if we dont have, then put it in that
return this.visited.get(node); //return the value
}
}
return null;
}
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
Node oldNode = head;
Node newNode = new Node(oldNode.val);
this.visited.put(oldNode, newNode);
while (oldNode != null) {
newNode.random = this.getCloneNode(oldNode.random); //newNode.random is a clone from oldNode.random
newNode.next = this.getCloneNode(oldNode.next);
oldNode = oldNode.next;
newNode = newNode.next;
}
return this.visited.get(head); //
}
}
相關文章
- 【Leetcode】138. Copy List with Random PointerLeetCoderandom
- Leetcode Copy List with Random PointerLeetCoderandom
- Leetcode-Copy List with Random PointerLeetCoderandom
- Copy List with Random Pointer leetcode javarandomLeetCodeJava
- LeetCode138:Copy List with Random PointerLeetCoderandom
- leetcode138: Copy List with Random PointerLeetCoderandom
- LeetCode 138:複製帶隨機指標的連結串列 Copy List with Random PointerLeetCode隨機指標random
- LeetCode 382 Linked List Random NodeLeetCoderandom
- LeetCode-Linked List Random NodeLeetCoderandom
- Leetcode - Random Pick IndexLeetCoderandomIndex
- LeetCode-Random Pick IndexLeetCoderandomIndex
- Leetcode Rotate ListLeetCode
- Leetcode Partition ListLeetCode
- [LeetCode] 528. Random Pick with WeightLeetCoderandom
- LeetCode-Reorder ListLeetCode
- Leetcode-Partition ListLeetCode
- Leetcode-Rotate ListLeetCode
- Leetcode-Sort ListLeetCode
- Partition List leetcode javaLeetCodeJava
- Rotate List leetcode javaLeetCodeJava
- Reorder List leetcode javaLeetCodeJava
- Sort List leetcode javaLeetCodeJava
- [LeetCode] 61. Rotate ListLeetCode
- Leetcode 61. Rotate ListLeetCode
- 【Leetcode】86. Partition ListLeetCode
- Leetcode Insertion Sort ListLeetCode
- leetcode Linked List CycleLeetCode
- [LeetCode] 148. Sort ListLeetCode
- LeetCode | 148. Sort ListLeetCode
- 【Leetcode】143. Reorder ListLeetCode
- LeetCode-Flatten Nested List IteratorLeetCode
- LeetCode148:Sort ListLeetCode
- LeetCode143:Reorder ListLeetCode
- Leetcode Reverse Linked List IILeetCode
- Leetcode Remove Duplicates from Sorted ListLeetCodeREM
- Leetcode Linked List Cycle IILeetCode
- LeetCode Delete Node in a Linked ListLeetCodedelete
- Leetcode Palindrome Linked ListLeetCode