The problem. Given the heads of two singly linked lists, headA and headB, return the node where the two lists join. If they never join, return null. Joining means sharing the same node in memory — from that node on, both lists are identical.
Input: A = [4, 1, 8, 4, 5], B = [5, 6, 1, 8, 4, 5], they meet at the node 8
Output: the node 8
Input: A = [2, 6, 4], B = [1, 5], no shared nodes
Output: nullCareful: both lists contain a 1, but those are different nodes. We compare node identity (a === b), never values.
Members only · free account
The rest of this lesson, with 5 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson