Bird
Raised Fist0
DSA Javaprogramming~10 mins

Intersection Point of Two Linked Lists in DSA Java - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if either linked list head is null.

DSA Java
if (headA == [1] || headB == null) {
    return null;
}
Drag options to blanks, or click blank then click option'
AheadA
B0
Cnull
DheadB
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of null for empty list check.
Checking only one list head instead of both.
2fill in blank
medium

Complete the code to find the length of a linked list.

DSA Java
int length = 0;
ListNode current = head;
while (current != [1]) {
    length++;
    current = current.next;
}
Drag options to blanks, or click blank then click option'
Anull
Bhead
Ccurrent
Dhead.next
Attempts:
3 left
💡 Hint
Common Mistakes
Using current.next instead of null in condition.
Using head instead of current in the loop condition.
3fill in blank
hard

Fix the error in advancing the longer list pointer by the difference in lengths.

DSA Java
while (diff > 0) {
    [1] = [1].next;
    diff--;
}
Drag options to blanks, or click blank then click option'
AheadB
Bshorter
CheadA
Dlonger
Attempts:
3 left
💡 Hint
Common Mistakes
Advancing the shorter list pointer instead.
Using headA or headB directly without assigning to longer.
4fill in blank
hard

Fill both blanks to check if two nodes are the same and move both pointers forward.

DSA Java
while (longer != null && shorter != null) {
    if (longer [1] shorter) {
        return longer;
    }
    longer = longer.[2];
    shorter = shorter.next;
}
Drag options to blanks, or click blank then click option'
A==
Bequals()
Cnext
Dprev
Attempts:
3 left
💡 Hint
Common Mistakes
Using equals() which checks value equality, not reference.
Using 'prev' which is not defined in singly linked lists.
5fill in blank
hard

Fill both blanks to initialize pointers and calculate length difference.

DSA Java
ListNode longer = headA;
ListNode shorter = headB;
int lenA = 0, lenB = 0;
ListNode currentA = headA;
ListNode currentB = headB;
while (currentA != [1]) {
    lenA++;
    currentA = currentA.next;
}
while (currentB != [2]) {
    lenB++;
    currentB = currentB.next;
}
int diff = lenA - lenB;
if (diff < 0) {
    longer = headB;
    shorter = headA;
    diff = -diff;
}
while (diff > 0) {
    longer = longer.next;
    diff--;
}
Drag options to blanks, or click blank then click option'
Anull
BheadA
CheadB
Dcurrent
Attempts:
3 left
💡 Hint
Common Mistakes
Using headA or headB in loop condition instead of current pointers.
Using an undefined variable 'current' in loop condition.

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