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 PointerLeetCode隨機指標random
- LeetCode 382 Linked List Random NodeLeetCoderandom
- [LeetCode] 528. Random Pick with WeightLeetCoderandom
- [LeetCode] 61. Rotate ListLeetCode
- [LeetCode] 148. Sort ListLeetCode
- LeetCode | 148. Sort ListLeetCode
- LeetCode | 141 linked list cycleLeetCode
- Leetcode 61. Rotate ListLeetCode
- Copy of a Copy of a Copy
- LeetCode | 147. Insertion Sort ListLeetCode
- [leetcode]linked-list-cycle-iiLeetCode
- Leetcode 206. Reverse Linked ListLeetCode
- Leetcode 234. Palindrome Linked ListLeetCode
- LeetCode之Odd Even Linked List(Kotlin)LeetCodeKotlin
- [LeetCode] 328. Odd Even Linked ListLeetCode
- Leetcode 142. Linked List Cycle IILeetCode
- LeetCode - Easy - 206. Reverse Linked ListLeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- Leetcode 237. Delete Node in a Linked ListLeetCodedelete
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- [LeetCode] 430. Flatten a Multilevel Doubly Linked ListLeetCode
- Leetcode 19 Remove Nth Node From End of ListLeetCodeREM
- [LeetCode] 2487. Remove Nodes From Linked ListLeetCodeREM
- LeetCode 83. Remove Duplicates from Sorted ListLeetCodeREM
- LeetCode 398 Random Pick Index(蓄水池抽樣典型例題)LeetCoderandomIndex
- Tagged Pointer 字串字串
- C++ pointerC++
- AP(Access Pointer)
- Two Pointer Method
- [LeetCode] 109. Convert Sorted List to Binary Search TreeLeetCode
- Java for LeetCode 109 Convert Sorted List to Binary Search TreeJavaLeetCode
- LeetCode Remove Nth Node From End of List(019)解法總結LeetCodeREM
- LeetCode707:設計連結串列 Design Linked ListLeetCode
- pointer-events屬性
- COPY & SYS
- PostgreSQL:COPYSQL
- 深copy
- Leetcode(Python3) 19. Remove Nth Node From End of ListLeetCodePythonREM