Skip to content

Intersection of Two Linked Lists

EasyLinked List
Decode it first. Clues, constraints, pattern and plan before any code.

You get the heads of two singly linked lists, a and b. At some point they may merge into one shared tail: from that node on, both lists use the very same node objects. Return the first shared node, or None (null in TypeScript) if the lists never meet. Equal values do not count; it must be the same node object. Do not change either list, and try for O(1) extra memory.

a:      4 -> 1 \
                 8 -> 4 -> 5
b: 5 -> 6 -> 1 /
                                →  the node holding 8
 
a: 2 -> 6 -> 4
b: 1 -> 5                       →  None

The tests build lists that share a tail and check that you return the exact first shared node.

Constraints

  • 0 <= number of nodes in each list <= 3 * 10^4
  • 1 <= Node.val <= 10^5
  • Neither list contains a cycle.