Intersection of Two Linked Lists

medium

Given the heads of two singly linked lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection, return null.

The intersection is defined by reference, not value — the two lists share the same node object from the intersection point onward (the tails are physically the same nodes).

Input encoding (3 lines): list A's non-shared prefix, list B's non-shared prefix, then the shared tail (empty ⇒ no intersection). The judge reports the index of your returned node within [A-prefix ++ B-prefix ++ shared] (-1 for null).

Constraints

- The number of nodes of listA is in the range [0, 3*10^4] - The number of nodes of listB is in the range [0, 3*10^4] - -10^5 <= Node.val <= 10^5 - The lists intersect by reference or not at all

Solve this problem →