Skip to content

Commit eb3e01d

Browse files
committed
Sync LeetCode submission Runtime - 41 ms (37.05%), Memory - 17.8 MB (6.14%)
1 parent 4ecd391 commit eb3e01d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<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>
2+
3+
<p>Construct a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_blank"><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>
4+
5+
<p>For example, if there are two nodes <code>X</code> and <code>Y</code> in the original list, where <code>X.random --&gt; Y</code>, then for the corresponding two nodes <code>x</code> and <code>y</code> in the copied list, <code>x.random --&gt; y</code>.</p>
6+
7+
<p>Return <em>the head of the copied linked list</em>.</p>
8+
9+
<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>
10+
11+
<ul>
12+
<li><code>val</code>: an integer representing <code>Node.val</code></li>
13+
<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>
14+
</ul>
15+
16+
<p>Your code will <strong>only</strong> be given the <code>head</code> of the original linked list.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong class="example">Example 1:</strong></p>
20+
<img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/e1.png" style="width: 700px; height: 142px;" />
21+
<pre>
22+
<strong>Input:</strong> head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
23+
<strong>Output:</strong> [[7,null],[13,0],[11,4],[10,2],[1,0]]
24+
</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
<img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/e2.png" style="width: 700px; height: 114px;" />
28+
<pre>
29+
<strong>Input:</strong> head = [[1,1],[2,1]]
30+
<strong>Output:</strong> [[1,1],[2,1]]
31+
</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/12/18/e3.png" style="width: 700px; height: 122px;" /></strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> head = [[3,null],[3,0],[3,null]]
39+
<strong>Output:</strong> [[3,null],[3,0],[3,null]]
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>0 &lt;= n &lt;= 1000</code></li>
47+
<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
48+
<li><code>Node.random</code> is <code>null</code> or is pointing to some node in the linked list.</li>
49+
</ul>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Approach 1: Recursive
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
"""
6+
# Definition for a Node.
7+
class Node:
8+
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
9+
self.val = int(x)
10+
self.next = next
11+
self.random = random
12+
"""
13+
14+
class Solution:
15+
def __init__(self) -> None:
16+
self.visited_hash = {}
17+
18+
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
19+
if head == None:
20+
return None
21+
22+
if head in self.visited_hash:
23+
return self.visited_hash[head]
24+
25+
node = Node(head.val, None, None)
26+
27+
self.visited_hash[head] = node
28+
29+
node.next = self.copyRandomList(head.next)
30+
node.random = self.copyRandomList(head.random)
31+
32+
return node
33+
34+

0 commit comments

Comments
 (0)