DEV Community

Alex Rivers
Alex Rivers

Posted on

**1. Problem Title**

1. Problem Title

Thursday, April 02, 2026 — Medium difficulty — Topic: Linked Lists



1. Problem Title

"The Secret Meeting Point"


2. Problem Statement

You are given two linked lists representing the paths of two spies. Each spy starts at a different location and walks along their respective paths. However, they eventually meet at a common node (the "secret meeting point"). Your task is to find this node where the two paths intersect.

If the paths do not intersect, return null.

This problem is inspired by real-world scenarios like route planning, network analysis, and detecting shared resources in distributed systems.


3. Examples

Example 1

Input:

  • List A: 1 → 2 → 3 → 4 → 5
  • List B: 6 → 7 → 4 → 5 Output: Node with value 4 Explanation: Both lists meet at node 4.

Example 2

Input:

  • List A: 1 → 2 → 3
  • List B: 4 → 5 → 6 Output: null Explanation: The lists do not intersect.

Example 3

Input:

  • List A: 1 → 2 → 3 → 4 → 5
  • List B: 3 → 4 → 5 Output: Node with value 3 Explanation: The lists merge at node 3.

4. Constraints

  • Time complexity: O(n) (linear time)
  • Space complexity: O(1) (constant space)
  • The linked lists may have cycles (follow-up challenge).

5. Hints

  1. Consider using two pointers to traverse both lists.
  2. Think about how to handle differences in the lengths of the two lists.

6. Solution Walkthrough

Step 1: Use two pointers, p1 and p2, starting at the heads of List A and List B, respectively.

Step 2: Traverse both lists. When a pointer reaches the end of its list, redirect it to the head of the other list.

Step 3: Continue traversal until the two pointers meet. This meeting point is the intersection node.

Step 4: If no intersection exists, the pointers will both reach null at the same time.

This approach works because the combined traversal ensures both pointers travel the same total distance, accounting for differences in list lengths.


7. Python Solution

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def getIntersectionNode(headA: ListNode, headB: ListNode) -> ListNode:
    if not headA or not headB:
        return None

    p1, p2 = headA, headB

    while p1 != p2:
        p1 = p1.next if p1.next else headB
        p2 = p2.next if p2.next else headA

    return p1
Enter fullscreen mode Exit fullscreen mode

8. JavaScript Solution

class ListNode {
    constructor(val = 0, next = null) {
        this.val = val;
        this.next = next;
    }
}

function getIntersectionNode(headA, headB) {
    if (!headA || !headB) return null;

    let p1 = headA, p2 = headB;

    while (p1 !== p2) {
        p1 = p1.next || headB;
        p2 = p2.next || headA;
    }

    return p1;
}
Enter fullscreen mode Exit fullscreen mode

9. Time/Space Complexity Analysis

  • Time Complexity: O(n) Each pointer traverses both lists at most once.
  • Space Complexity: O(1) No extra data structures are used.

10. Follow-up Challenge (Hard)

Problem: Modify the solution to handle cyclic linked lists. For example, if both lists have cycles and intersect at a node inside the cycle, find the intersection point.

Constraints:

  • Time complexity: O(n)
  • Space complexity: O(1)

Hint: Use Floyd’s Cycle Detection Algorithm in combination with the two-pointer approach.


This problem is a classic interview favorite, testing your ability to think creatively about pointer manipulation and edge cases. Happy coding! 🚀


Find this helpful? Follow for daily coding challenges!

Top comments (0)