Duplicate Linked List with Arbitrary Pointers
Linked ListsAuto-graded · 20 test casesLast updated
Demonstrate deep copying of a sequential structural chain featuring auxiliary target links via node-level or serialized array transformations.
A specialized singly linked list structure encodes node sequence values alongside auxiliary pointers. In addition to a standard sequential next reference connecting node $i$ to node $i+1$, each node contains a random pointer pointing to any node in the structure, or to null.
In the serialized representation:
values[i]represents the stored data value of node $i$.random_indices[i]contains the zero-based index of the node targeted by node $i$'srandompointer, or-1if the pointer is null.
Your task is to implement a function that constructs a complete deep copy of this serialized structure. The returned output must be a two-element list containing brand-new arrays for values and random_indices respectively. The new data structures must fully mirror the content and relational references of the original input without sharing any underlying array references (i.e. mutating the returned structures must not affect the original inputs).
Constraints
- $0 \le n \le 10^4$, where $n$ is the length of
values(andrandom_indices). - $-10^4 \le \text{values}[i] \le 10^4$ for all $0 \le i < n$.
- $-1 \le \text{random_indices}[i] < n$ for all $0 \le i < n$.
- The length of
valuesis guaranteed to match the length ofrandom_indices.
Examples
Example 1
Input: duplicate_random_linked_list([1, 2, 3], [2, -1, 0])
Output: [[1, 2, 3], [2, -1, 0]]
Example 2
Input: duplicate_random_linked_list([7], [0])
Output: [[7], [0]]
Example 3
Input: duplicate_random_linked_list([4, 5, 6], [-1, -1, -1])
Output: [[4, 5, 6], [-1, -1, -1]]
Example 4
Input: duplicate_random_linked_list([10, 10, -3, 8], [-1, 0, 3, 1])
Output: [[10, 10, -3, 8], [-1, 0, 3, 1]]
Hints
Hint 1
In a serialized array format, duplicating node data and maintaining target index mappings requires creating independent instances of the input lists.
Hint 2
Notice how relative target positions (indices) remain identical between the source list and its deep copy.
Hint 3
Ensure your function handles edge cases like empty lists, self-referencing nodes, and list elements pointing to null targets (-1).
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
When duplicating a linked structure represented directly with explicit Node objects, two primary techniques exist:
- Hash Map Approach: A traversal pass instantiates a new clone for every original node, storing them in a hash map
map[original_node] = cloned_node. A second pass linksclone.nextandclone.randomusing the map lookups. This operates in $O(n)$ time and $O(n)$ auxiliary memory. - Interleaving (Weaving) Approach: Each cloned node is placed immediately after its source node ($A \to A' \to B \to B'$). The cloned target links are set via
A'.random = A.random.next. Finally, the lists are unwoven back into original and copied chains. This achieves $O(n)$ time with $O(1)$ auxiliary space.
Under the array-serialized format used by the test runner:
- The
nextrelationship is implicit via list indices ($i \to i + 1$). - The
randompointers are index references (random_indices[i]).
Because positional indices remain invariant under structural deep copies, creating fresh, independent copies of values and random_indices creates an identical, completely isolated deep copy.
Complexity Analysis
- Time Complexity: $\mathcal{O}(n)$, where $n$ is the total number of nodes, as copying each list requires a single linear pass.
- Space Complexity: $\mathcal{O}(n)$ auxiliary space to allocate and hold the newly created lists forming the output structure.
Implementation
from typing import List
def duplicate_random_linked_list(values: List[int], random_indices: List[int]) -> List[List[int]]:
# Construct completely independent list instances
copied_values = list(values)
copied_random_indices = list(random_indices)
return [copied_values, copied_random_indices]