This problem asks us to find the point where two linked lists merge into a single shared tail. Intersection here means identity, not value. Two nodes intersect when they are the same node in memory, not two different nodes that happen to hold the same value. Once two lists converge at an intersection node, they share every node from there to the end.
The main challenge is that the two lists can have different lengths. If list A has 5 nodes before the intersection and list B has 3, walking both pointers forward in lockstep will not line them up. The core question becomes how to synchronize two pointers that start at different distances from the intersection point.
1 <= m, n <= 3 * 10^4 → With up to 30,000 nodes per list, O(m * n) would mean nearly a billion operations, which is too slow. We should aim for O(m + n).1 <= Node.val <= 10^5 → Values are positive integers, but remember that intersection is about node identity, not node values. Two nodes with value 8 are not the same unless they're the same object.If we record every node from one list, then checking whether a node from the other list was already seen becomes an O(1) lookup. A hash set provides that lookup. Walk through all of list A and store every node reference in a set. Then walk through list B and check each node against the set. The first node found in the set is the intersection point, because from that node onward both lists share the same nodes.
This is correct but uses O(m) extra space for the set, which does not satisfy the follow-up constraint.
Input:
In this example the two lists merge at the node with value 8, so listA is 4 -> 1 -> 8 -> 4 -> 5 and listB is 5 -> 6 -> 1 -> 8 -> 4 -> 5, where the trailing 8 -> 4 -> 5 is the shared tail.
Pass 1 stores all five listA nodes in the set. Pass 2 walks listB and checks each node against the set. The first three nodes (5, 6, 1) belong only to listB and are not in the set. The next node is the shared node 8, which is in the set, so we return it immediately:
This approach works correctly but uses O(m) extra space. The next approach aligns two pointers so they arrive at the intersection at the same time, without storing any nodes.
The two lists have different lengths, so two pointers starting at their respective heads and walking forward in lockstep will not reach the intersection at the same time. The fix is to have each pointer, upon reaching the end of its list, continue from the head of the other list.
Consider the total distance each pointer travels. Pointer A walks through list A (length m), then switches to list B (length n). Pointer B walks through list B (length n), then switches to list A (length m). Both pointers travel exactly m + n nodes total. The difference in lengths is absorbed during the first pass. On the second pass, both pointers are the same distance from the intersection point, so they meet there.
If there's no intersection, both pointers reach null at the same time after traveling m + n nodes each.
Let the portion of list A before the intersection be a nodes, the portion of list B before the intersection be b nodes, and the shared tail be c nodes. Pointer A travels a + c + b nodes before reaching the intersection on its second pass. Pointer B travels b + c + a nodes before reaching it. Since a + c + b = b + c + a, both pointers arrive at the same node after the same number of steps.
When there is no intersection, c = 0. Pointer A travels a + b nodes and reaches null; pointer B travels b + a nodes and also reaches null. The two pointers become equal (both null) at that point, the loop condition pA != pB becomes false, and we return null. Both cases use the same loop with no special-casing.
pA at headA and pointer pB at headB.pA reaches the end of list A (null), redirect it to headB.pB reaches the end of list B (null), redirect it to headA.pA and pB will meet at the intersection node.