Clone a Linked List with Arbitrary Jump Links
Linked ListsAuto-graded · 17 test casesLast updated
Build an independent duplicate of a linked list whose nodes carry both a next link and an arbitrary jump link, using a serialized list-of-records format.
Clone a Linked List with Jump Links
A singly linked list has n nodes. Each node stores an integer val, a next link to the following node (or None at the tail), and a jump link that points to any node in the same list (possibly itself) or is None.
Your task is to build a deep copy of such a list: n brand-new nodes, where the copy of each node has the same value, and its next and jump links point to the copies of the nodes that the original links pointed to. A None link stays None. No link in the copy may point into the original list, and the original must not be changed.
Serialized format
The judge calls your function with the list in serialized form, and expects the serialized form of your copy back.
- The input
recordsis a list ofnentries, one per node, in order from head to tail. - Entry
iis[val, jump_index], wherevalis the value of nodei, andjump_indexis the 0-based position of the node that nodei's jump link points to, orNoneif the jump link isNone. - The
nextlink of nodeigoes to nodei + 1; the last node'snextisNone.
Your function should:
- Build real linked nodes from
records. - Create a deep copy of that list.
- Serialize the copy by walking its own
nextlinks from its head, giving each node[val, jump_index]wherejump_indexis the position (in the copy) of the node its jump link targets, orNone.
Return the resulting list of records. If records is empty, return an empty list.
Because the copy mirrors the original exactly, the returned list has the same content as the input when the copy is built correctly.
Constraints
0 <= n <= 10^4-10^4 <= val <= 10^4jump_indexisNoneor an integer in[0, n - 1].- Several nodes may share the same value; nodes are distinguished by identity, not by value.
- A node's jump link may point to itself, backward, or forward.
Examples
Example 1
Input: clone_jump_list([[7, None], [13, 0], [11, 4], [10, 2], [1, 0]])
Output: [[7, None], [13, 0], [11, 4], [10, 2], [1, 0]]
Example 2
Input: clone_jump_list([[1, 1], [2, 1]])
Output: [[1, 1], [2, 1]]
Hints
Hint 1
When you copy a node, its jump target may not have been copied yet. Think about how to separate creating nodes from connecting them.
Hint 2
Use a dictionary keyed by the original node object (identity, not value) that maps to its new copy. Create all copies first, then wire next and jump in a second pass.
Hint 3
Treat None specially: looking up None should yield None. For serialization, build a reverse map from each copied node to its position as you walk the copy.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The difficulty is the jump link: when copying a node, its target may be a node that has not been copied yet, or one that was copied earlier. What we need is a fast answer to "which new node is the copy of this original node?"
A dictionary from original node to copied node answers that:
- Build the original list from the records: create one node per record, link
nextpointers in order, and set eachjumpusing the stored index. - Pass 1: create a fresh node with the same value for every original and store
original -> copy. No links are set yet. - Pass 2: for each original node, set
copy.next = map.get(original.next)andcopy.jump = map.get(original.jump).dict.get(None)returnsNone, which handles missing links cleanly. - Serialize the copy by walking from the copy's head along its own
nextlinks. A reverse map from copied node to position converts each jump link back into an index.
Why it is correct
- Exactly
nnew nodes are created, one per original. - Every link assigned in pass 2 is either
Noneor a value from the map, which only holds new nodes, so nothing points into the original. - Since the map is a bijection between original and copied nodes, a link
x -> yin the original becomescopy(x) -> copy(y)in the copy. Self-links are preserved the same way. - The map is keyed by node identity, so duplicate values are not a problem.
- Serializing from the copy's own links means the output reflects the copy's real structure, which equals the input when the copy is right.
- The empty input is handled by returning
[]early.
Complexity
- Time: O(n): each stage is a single linear pass with O(1) dictionary operations.
- Space: O(n) for the original-to-copy map and the reverse index map (the copied nodes themselves are the output).
Reference solution
from typing import List, Optional
class Node:
def __init__(self, val, next_node=None, jump=None):
self.val = val
self.next = next_node
self.jump = jump
def clone_jump_list(records: List[List[Optional[int]]]) -> List[List[Optional[int]]]:
if not records:
return []
# Build the original list from the serialized records.
originals = [Node(r[0]) for r in records]
for i in range(len(originals) - 1):
originals[i].next = originals[i + 1]
for i, r in enumerate(records):
j = r[1]
originals[i].jump = None if j is None else originals[j]
# Deep copy using an identity-keyed map.
copies = {node: Node(node.val) for node in originals}
for node in originals:
c = copies[node]
c.next = copies.get(node.next)
c.jump = copies.get(node.jump)
# Serialize the copy by walking its own next links.
index_of = {copies[node]: i for i, node in enumerate(originals)}
result = []
cur = copies[originals[0]]
while cur is not None:
j = None if cur.jump is None else index_of[cur.jump]
result.append([cur.val, j])
cur = cur.next
return result