Copy List with Random Pointer - Problem

Imagine you have a special linked list where each node not only points to the next node, but also has a random pointer that can point to any node in the list (or be null). Your task is to create a deep copy of this entire structure.

A deep copy means creating completely new nodes with the same values, where:

  • Each new node's next pointer points to the corresponding new node
  • Each new node's random pointer points to the corresponding new node
  • No pointers in the new list should reference the original list

For example, if the original list has nodes X and Y where X.random โ†’ Y, then in your copied list with nodes x and y, we should have x.random โ†’ y.

Input: Head of the original linked list
Output: Head of the deep copied linked list

Note: The problem description shows the list as [val, random_index] pairs for clarity, but your function only receives the head node.

Input & Output

example_1.py โ€” Basic Case
$ Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
โ€บ Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
๐Ÿ’ก Note: The copied list maintains the same structure: node 13 points to node 7 (index 0), node 11 points to node 1 (index 4), node 10 points to node 11 (index 2), and node 1 points to node 7 (index 0).
example_2.py โ€” Two Node Case
$ Input: head = [[1,1],[2,1]]
โ€บ Output: [[1,1],[2,1]]
๐Ÿ’ก Note: Both nodes point to the second node (index 1) as their random pointer. The copy preserves this relationship with new nodes.
example_3.py โ€” Single Node
$ Input: head = [[1,null]]
โ€บ Output: [[1,null]]
๐Ÿ’ก Note: A single node with no random pointer. The copy is simply a new node with the same value and null random pointer.

Visualization

Tap to expand
Deep Copy with Hash MapOriginal Office713111ID Card DirectoryOriginal(7) โ†’ NewCard(7)Original(13) โ†’ NewCard(13)Original(11) โ†’ NewCard(11)Original(1) โ†’ NewCard(1)Instant Lookup: O(1)New Office713111๐Ÿ”„ Cloning Process1. Scan original employee2. Create/find new card3. Link relationships4. Perfect copy!
Understanding the Visualization
1
Encounter Original Node
When we meet an employee in the original office, we check if they have an ID card in the new office
2
Create Card On-Demand
If no card exists, we create one immediately with the same info
3
Link Relationships
Set up their direct report (next) and mentor (random) relationships using the same process
4
Complete Network
By the end, we have a complete duplicate organizational structure
Key Takeaway
๐ŸŽฏ Key Insight: Hash map provides instant mapping between original and copied nodes, enabling efficient deep copy in just one pass through the list.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the list, hash map operations are O(1) on average

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map stores mapping for all n nodes

n
2n
โšก Linearithmic Space

Constraints

  • 0 โ‰ค n โ‰ค 1000
  • -104 โ‰ค Node.val โ‰ค 104
  • Node.random is null or is pointing to some node in the linked list
  • You must return the copy of the given head as a reference to the cloned list
Asked in
Amazon 42 Microsoft 38 Google 35 Meta 29 Apple 18
127.4K Views
High Frequency
~18 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen