Class Solution
java.lang.Object
g0101_0200.s0138_copy_list_with_random_pointer.Solution
138 - Copy List with Random Pointer.<p>Medium</p>
<p>A linked list of length <code>n</code> is given such that each node contains an additional random pointer, which could point to any node in the list, or <code>null</code>.</p>
<p>Construct a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_top"><strong>deep copy</strong></a> of the list. The deep copy should consist of exactly <code>n</code> <strong>brand new</strong> nodes, where each new node has its value set to the value of its corresponding original node. Both the <code>next</code> and <code>random</code> pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. <strong>None of the pointers in the new list should point to nodes in the original list</strong>.</p>
<p>For example, if there are two nodes <code>X</code> and <code>Y</code> in the original list, where <code>X.random --> Y</code>, then for the corresponding two nodes <code>x</code> and <code>y</code> in the copied list, <code>x.random --> y</code>.</p>
<p>Return <em>the head of the copied linked list</em>.</p>
<p>The linked list is represented in the input/output as a list of <code>n</code> nodes. Each node is represented as a pair of <code>[val, random_index]</code> where:</p>
<ul>
<li><code>val</code>: an integer representing <code>Node.val</code></li>
<li><code>random_index</code>: the index of the node (range from <code>0</code> to <code>n-1</code>) that the <code>random</code> pointer points to, or <code>null</code> if it does not point to any node.</li>
</ul>
<p>Your code will <strong>only</strong> be given the <code>head</code> of the original linked list.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2019/12/18/e1.png" alt="" /></p>
<p><strong>Input:</strong> head = [[7,null],[13,0],[11,4],[10,2],[1,0]]</p>
<p><strong>Output:</strong> [[7,null],[13,0],[11,4],[10,2],[1,0]]</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2019/12/18/e2.png" alt="" /></p>
<p><strong>Input:</strong> head = [[1,1],[2,1]]</p>
<p><strong>Output:</strong> [[1,1],[2,1]]</p>
<p><strong>Example 3:</strong></p>
<p><strong><img src="https://assets.leetcode.com/uploads/2019/12/18/e3.png" alt="" /></strong></p>
<p><strong>Input:</strong> head = [[3,null],[3,0],[3,null]]</p>
<p><strong>Output:</strong> [[3,null],[3,0],[3,null]]</p>
<p><strong>Example 4:</strong></p>
<p><strong>Input:</strong> head = []</p>
<p><strong>Output:</strong> []</p>
<p><strong>Explanation:</strong> The given linked list is empty (null pointer), so return null.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>0 <= n <= 1000</code></li>
<li><code>-10000 <= Node.val <= 10000</code></li>
<li><code>Node.random</code> is <code>null</code> or is pointing to some node in the linked list.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
copyRandomList
-