Bird
Raised Fist0
DSA Javaprogramming~5 mins

Intersection Point of Two Linked Lists in DSA Java - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: Intersection Point of Two Linked Lists
O(m + n)
Understanding Time Complexity

We want to know how long it takes to find where two linked lists meet.

How does the time grow as the lists get longer?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode a = headA, b = headB;
    while (a != b) {
        a = (a == null) ? headB : a.next;
        b = (b == null) ? headA : b.next;
    }
    return a;
}
    

This code finds the node where two linked lists intersect by switching heads when reaching the end.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Traversing nodes in both linked lists.
  • How many times: Each pointer moves through each list at most twice.
How Execution Grows With Input

As the lists get longer, the pointers travel through all nodes in both lists.

Input Size (n)Approx. Operations
10About 20 steps (10 + 10)
100About 200 steps (100 + 100)
1000About 2000 steps (1000 + 1000)

Pattern observation: The total steps grow roughly proportional to the sum of the lengths of both lists.

Final Time Complexity

Time Complexity: O(m + n)

This means the time grows linearly with the total number of nodes in both lists combined.

Common Mistake

[X] Wrong: "The loop runs forever or more than twice the list length."

[OK] Correct: Because each pointer switches lists once, they cover all nodes at most twice, so the loop ends.

Interview Connect

Understanding this helps you explain how to efficiently find shared parts of linked lists, a common real-world problem.

Self-Check

"What if we used a hash set to store visited nodes instead? How would the time complexity change?"

Practice

(1/5)
1. What does the term intersection point of two linked lists mean?
easy
A. The node where both linked lists share the same reference (memory address).
B. The node where the values of both linked lists are equal.
C. The first node of the longer linked list.
D. The last node of the shorter linked list.

Solution

  1. Step 1: Understand linked list nodes

    Each node in a linked list has a value and a reference (memory address) to the next node.
  2. Step 2: Define intersection point

    The intersection point is where two linked lists share the exact same node reference, meaning they merge at that node.
  3. Final Answer:

    The node where both linked lists share the same reference (memory address). -> Option A
  4. Quick Check:

    Intersection means shared node reference [OK]
Hint: Intersection means same node object, not just same value [OK]
Common Mistakes:
  • Confusing same value with same node reference
  • Thinking intersection is about list length
  • Assuming intersection is always at the last node
2. Which of the following Java method signatures correctly represents a function to find the intersection point of two singly linked lists?
easy
A. public ListNode getIntersectionNode(int headA, int headB)
B. public void getIntersectionNode(ListNode headA, ListNode headB)
C. public int getIntersectionNode(ListNode headA, ListNode headB)
D. public ListNode getIntersectionNode(ListNode headA, ListNode headB)

Solution

  1. Step 1: Understand method return type

    The method should return the intersecting node, so return type must be ListNode.
  2. Step 2: Check parameter types

    Parameters must be the heads of the two linked lists, which are ListNode references.
  3. Final Answer:

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) -> Option D
  4. Quick Check:

    Return node, input heads as ListNode [OK]
Hint: Return the intersecting node, input heads as ListNode [OK]
Common Mistakes:
  • Using void return type instead of ListNode
  • Using primitive types instead of ListNode
  • Incorrect parameter types
3. Consider the following Java code snippet to find the intersection point of two linked lists:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode a = headA, b = headB;
    while (a != b) {
        a = (a == null) ? headB : a.next;
        b = (b == null) ? headA : b.next;
    }
    return a;
}

If the two linked lists intersect at node with value 8, what will be the output when printing getIntersectionNode(headA, headB).val?
medium
A. 0
B. 8
C. null
D. Runtime error

Solution

  1. Step 1: Understand the two-pointer approach

    Two pointers traverse each list; when one reaches end, it switches to the other list's head. They meet at intersection.
  2. Step 2: Identify intersection value

    Since lists intersect at node with value 8, pointers meet there and return that node.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    Two-pointer meet at intersection node value 8 [OK]
Hint: Two pointers meet at intersection node value [OK]
Common Mistakes:
  • Assuming output is null if lists differ in length
  • Confusing node value with node reference
  • Not understanding pointer switching logic
4. The following code attempts to find the intersection point of two linked lists but has a bug:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    while (headA != headB) {
        headA = headA.next;
        headB = headB.next;
    }
    return headA;
}

What is the main problem with this code?
medium
A. It does not handle different list lengths, causing NullPointerException.
B. It uses incorrect comparison operator for nodes.
C. It returns the wrong node even if lists intersect.
D. It modifies the original lists, which is not allowed.

Solution

  1. Step 1: Analyze pointer movement

    Both pointers move forward without switching lists, so if lengths differ, one pointer reaches null first.
  2. Step 2: Identify error cause

    When one pointer is null and the other is not, accessing next causes NullPointerException.
  3. Final Answer:

    It does not handle different list lengths, causing NullPointerException. -> Option A
  4. Quick Check:

    Different lengths cause null access error [OK]
Hint: Handle different lengths by switching pointers [OK]
Common Mistakes:
  • Ignoring list length difference
  • Assuming nodes with same value are intersection
  • Not checking for null before accessing next
5. You have two singly linked lists that intersect. List A has length 5, List B has length 7. Which approach correctly finds the intersection point in O(n) time without extra space?
hard
A. Use a hash set to store nodes of List A, then check nodes of List B.
B. Reverse both lists and compare nodes from the start.
C. Calculate lengths, advance longer list by difference, then move both pointers together.
D. Traverse both lists simultaneously without adjusting for length difference.

Solution

  1. Step 1: Calculate lengths and difference

    Find lengths of both lists and compute difference (7 - 5 = 2).
  2. Step 2: Advance longer list pointer

    Move pointer of longer list (List B) ahead by difference to align both pointers equally from intersection.
  3. Step 3: Move both pointers together to find intersection

    Advance both pointers one step at a time until they meet at intersection node.
  4. Final Answer:

    Calculate lengths, advance longer list by difference, then move both pointers together. -> Option C
  5. Quick Check:

    Align pointers by length difference for O(n) solution [OK]
Hint: Advance longer list by length difference before simultaneous traversal [OK]
Common Mistakes:
  • Using extra space like hash sets (not O(1) space)
  • Reversing lists which is inefficient and modifies data
  • Not adjusting for length difference causing wrong results
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard