Linked List Intersection Finder - Problem

Given two singly linked lists that may merge at some point, find the intersection node where they first converge.

If the two linked lists have no intersection at all, return null.

The linked lists must retain their original structure after the function returns.

You may assume there are no cycles anywhere in the entire linked structure.

Each value on each node is in the range [1, 109].

Input & Output

Example 1 — Lists Intersect at Node 4
$ Input: listA = [1,8,4], listB = [5,4], intersectVal = 4
Output: 4
💡 Note: List A: 1→8→4, List B: 5→4. They intersect at the node with value 4. The intersection node is where the linked lists merge, not just matching values.
Example 2 — No Intersection
$ Input: listA = [2,6,4], listB = [1,5], intersectVal = null
Output: null
💡 Note: The two linked lists do not intersect at all, so return null. Even if they had the same values, they would need to share actual node references.
Example 3 — Intersection at Head
$ Input: listA = [3], listB = [3], intersectVal = 3
Output: 3
💡 Note: Both lists consist of the same single node, so they intersect at the head node with value 3.

Constraints

  • The number of nodes of listA is in the range [0, 3 × 104]
  • The number of nodes of listB is in the range [0, 3 × 104]
  • 1 ≤ Node.val ≤ 105
  • 0 ≤ skipA ≤ m
  • 0 ≤ skipB ≤ n

Visualization

Tap to expand
INPUT184List A: 1→8→454List B: 5→4Node 4 is sharedALGORITHM1Start pointers at heads2Move both forward3At end, switch to other list4Meet at intersectionTwo Pointer TechniqueBoth travel same distance:A: 1→8→4→5→4B: 5→4→1→8→4RESULT4Intersection NodeBoth pointers meet hereafter traveling equal distanceOutput: 4Key Insight:By switching paths when reaching the end, both pointers travel the same total distance (m+n) and meet at the intersection point.TutorialsPoint - Linked List Intersection Finder | Two Pointers Technique
Asked in
Amazon 45 Microsoft 38 Facebook 32 Apple 28
78.4K Views
High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen