Bird
Raised Fist0
DSA Javaprogramming~20 mins

Intersection Point of Two Linked Lists in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Intersection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Intersection Point Detection
What will be the output of the following Java code that finds the intersection point of two linked lists?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

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

    public static void main(String[] args) {
        Node common = new Node(8);
        common.next = new Node(10);

        Node headA = new Node(3);
        headA.next = new Node(7);
        headA.next.next = common;

        Node headB = new Node(99);
        headB.next = new Node(1);
        headB.next.next = common;

        Node intersection = getIntersectionNode(headA, headB);
        if (intersection != null) {
            System.out.println(intersection.data);
        } else {
            System.out.println("No intersection");
        }
    }
}
ANo intersection
B10
C8
D7
Attempts:
2 left
💡 Hint
Trace the pointers moving through both lists until they meet.
🧠 Conceptual
intermediate
1:30remaining
Understanding Intersection Point Logic
Why does switching pointers to the other list's head help find the intersection point in two linked lists?
AIt equalizes the path lengths by traversing both lists fully, so pointers align at intersection.
BIt skips nodes to speed up the search for intersection.
CIt resets the pointers to start over, ensuring no infinite loops.
DIt merges the two lists into one to find the intersection.
Attempts:
2 left
💡 Hint
Think about how different list lengths affect pointer positions.
🔧 Debug
advanced
2:00remaining
Identify the Error in Intersection Detection Code
What error will occur when running this code to find the intersection point of two linked lists?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

public class Test {
    static Node getIntersectionNode(Node headA, Node headB) {
        Node a = headA, b = headB;
        while (a != b) {
            if (a == null && b == null) return null;
            if (a == null) a = headB;
            else a = a.next;
            if (b == null) b = headA;
            else b = b.next;
        }
        return a;
    }
}
AInfinite loop because pointers never meet
BNullPointerException when a or b is null and next is accessed
CCompilation error due to missing return statement
DCorrect output with intersection node
Attempts:
2 left
💡 Hint
Check the order of pointer moves and null checks.
❓ Predict Output
advanced
2:00remaining
Output When Lists Do Not Intersect
What will be the output of this code when two linked lists have no intersection?
DSA Java
class Node {
    int data;
    Node next;
    Node(int d) { data = d; next = null; }
}

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

    public static void main(String[] args) {
        Node headA = new Node(1);
        headA.next = new Node(2);
        headA.next.next = new Node(3);

        Node headB = new Node(4);
        headB.next = new Node(5);
        headB.next.next = new Node(6);

        Node intersection = getIntersectionNode(headA, headB);
        if (intersection != null) {
            System.out.println(intersection.data);
        } else {
            System.out.println("No intersection");
        }
    }
}
ANo intersection
B1
C4
DNullPointerException
Attempts:
2 left
💡 Hint
If no intersection, pointers eventually become null together.
🧠 Conceptual
expert
1:30remaining
Time Complexity of Intersection Point Algorithm
What is the time complexity of the two-pointer algorithm to find the intersection point of two singly linked lists?
AO(1), constant time
BO(m * n), where m and n are lengths of the two lists
CO(max(m, n)^2), where m and n are lengths of the two lists
DO(m + n), where m and n are lengths of the two lists
Attempts:
2 left
💡 Hint
Consider how many times each pointer traverses nodes.

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