Find Where Two Linked Lists Merge
Linked ListsAuto-graded · 17 test casesLast updated
Tests pointer manipulation and list traversal to locate the merge point of two singly linked lists in linear time with constant space.
You are given two singly linked lists that may share a common tail. Each list is represented by a private prefix followed by a shared suffix. Your task is to identify the index at which the shared portion begins within the first list.
More precisely, you receive:
prefix_a: the values unique to the first list, before any merging occursprefix_b: the values unique to the second list, before any merging occursshared_suffix: the values that both lists share after they merge
If the lists do not merge (the shared suffix is empty), return None. Otherwise, return the zero-based index in the first list where the shared suffix starts.
For instance, if the first list is [4, 1, 8, 4, 5] and the shared portion begins at index 2, the private prefix is [4, 1] and the shared suffix is [8, 4, 5].
The index you return is always len(prefix_a) when a shared suffix exists, because the suffix immediately follows the prefix in the concatenated list.
Constraints
0 ≤ len(prefix_a), len(prefix_b) ≤ 10⁴0 ≤ len(shared_suffix) ≤ 10⁴- All values are integers in the range
[-10⁹, 10⁹] - The concatenated lists each have at most
2 × 10⁴nodes total - If
shared_suffixis empty, the lists do not intersect
Examples
Example 1
Input: find_merge_index([4, 1], [5, 6, 1], [8, 4, 5])
Output: 2
Example 2
Input: find_merge_index([2, 6, 4], [1, 5], [])
Output: None
Hints
Hint 1
The merge point is where both lists begin sharing the exact same sequence of nodes, not just matching values.
Hint 2
When a shared suffix exists, it always starts immediately after the private prefix of each list.
Hint 3
The index of the merge point in the first list is simply the length of its private prefix.
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Two linked lists that merge share a common tail from the merge point onward. Once they join at a node, every subsequent node belongs to both lists—they cannot diverge again, forming a Y-shape structure.
When you represent each list as a private prefix followed by a shared suffix:
- The first list becomes
prefix_a + shared_suffix - The second list becomes
prefix_b + shared_suffix
The merge point in the first list is the position where shared_suffix starts. Because the suffix immediately follows the prefix, this position is exactly the length of prefix_a.
If the shared suffix is empty, the lists never merge, and we return None.
Why This Works
The problem's structure guarantees that:
- Any shared nodes form a contiguous suffix (no branching after merge)
- The merge index in the first list is the boundary between its private prefix and the shared portion
- This boundary occurs at index
len(prefix_a)(zero-indexed)
For example, with prefix_a = [4, 1] and shared_suffix = [8, 4, 5]:
- Index 0: value 4 (from prefix)
- Index 1: value 1 (from prefix)
- Index 2: value 8 (first shared node) ← merge point
Complexity
- Time: O(1) — we only check if the suffix is empty and compute a length
- Space: O(1) — we use no additional data structures
Implementation
from typing import List, Optional
def find_merge_index(prefix_a: List[int], prefix_b: List[int], shared_suffix: List[int]) -> Optional[int]:
# When a shared suffix exists, it begins immediately after the private prefix.
# The index is therefore the length of prefix_a.
if not shared_suffix:
return None
return len(prefix_a)
This solution directly exploits the problem's representation: since the shared suffix is explicitly provided, we need only determine where it begins in the concatenated first list. The answer is always the length of the private prefix when a merge exists.