Bird
Raised Fist0
DSA C++programming~10 mins

Intersection Point of Two Linked Lists in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Intersection Point of Two Linked Lists
Start with two heads: headA, headB
↓
Traverse both lists simultaneously
↓
If pointerA reaches end, switch to headB
↓
If pointerB reaches end, switch to headA
↓
Check if pointerA == pointerB
↓
Intersection
↓
Repeat until intersection or both null
Start traversing both lists with two pointers. When a pointer reaches the end, switch it to the other list's head. Continue until pointers meet or both reach null.
Execution Sample
DSA C++
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
  ListNode* a = headA;
  ListNode* b = headB;
  while (a != b) {
    a = a ? a->next : headB;
    b = b ? b->next : headA;
  }
  return a;
}
This code finds the intersection node of two linked lists by switching pointers when they reach the end.
Execution Table
StepOperationPointer APointer BVisual State
1StartNode A1Node B1List A: A1 -> A2 -> C1 -> C2 -> C3 List B: B1 -> B2 -> B3 -> C1 -> C2 -> C3
2Compare pointersA1B1No match, move both forward
3Move pointersA2B2Pointers moved to next nodes
4Compare pointersA2B2No match, move both forward
5Move pointersC1B3Pointers moved to next nodes
6Compare pointersC1B3No match, move both forward
7Move pointersC2C1Pointers moved to next nodes
8Compare pointersC2C1No match, move both forward
9Move pointersC3C2Pointers moved to next nodes
10Compare pointersC3C2No match, move both forward
11Move pointersnullC3Pointer A reached end, switch to headB; Pointer B moved forward
12Compare pointersB1C3No match, move both forward
13Move pointersB2nullPointer A moved forward; Pointer B reached end, switch to headA
14Compare pointersB2A1No match, move both forward
15Move pointersB3A2Pointers moved to next nodes
16Compare pointersB3A2No match, move both forward
17Move pointersC1C1Pointers moved to next nodes
18Compare pointersC1C1Match found! Intersection at C1
19ReturnC1C1Intersection node returned
💡 Pointers meet at node C1, intersection found.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10After 11After 12After 13After 14After 15After 16After 17Final
Pointer AA1A2C1C2C3nullB1B2B3C1nullB1B2B2B3B3C1C1C1
Pointer BB1B2B3C1C2C3C3nullA1A2C1C3nullA1A2A2C1C1C1
Key Moments - 3 Insights
Why do we switch pointer A to headB when it reaches the end of list A?
Switching pointer A to headB ensures both pointers traverse equal total lengths, allowing them to meet at the intersection if it exists. See execution_table steps 11 and 13 where pointers switch lists.
What happens if the lists do not intersect?
Both pointers will eventually become null after traversing both lists fully, causing the loop to exit without finding an intersection. This is implied by the exit condition in the code and the loop ending when a == b (both null).
Why do we compare pointers directly instead of node values?
We compare pointers because intersection means the exact same node in memory, not just equal values. Execution_table step 18 shows pointers matching by reference, not value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 11, what happens to pointer A?
APointer A moves to next node in list A
BPointer A reaches end of list A and switches to headB
CPointer A becomes null and stops
DPointer A moves to headA again
💡 Hint
Check the 'Operation' and 'Pointer A' columns at step 11 in execution_table
At which step do pointers first point to the intersection node?
AStep 18
BStep 15
CStep 10
DStep 19
💡 Hint
Look for the first 'Match found' in the 'Operation' column of execution_table
If list B was shorter and had no intersection, what would happen to the pointers?
APointer A would reach null but pointer B would not
BThey would loop infinitely
CThey would meet at null after traversing both lists
DThey would meet at the last node of list A
💡 Hint
Refer to key_moments about no intersection and exit conditions
Concept Snapshot
Intersection Point of Two Linked Lists:
- Use two pointers starting at heads of each list.
- Move pointers forward; switch to other list head when reaching end.
- Pointers meet at intersection or both become null.
- Compare pointers by reference, not value.
- Time: O(m+n), Space: O(1).
Full Transcript
To find the intersection point of two linked lists, we use two pointers starting at the heads of each list. We move both pointers forward one node at a time. When a pointer reaches the end of its list, it switches to the head of the other list. This way, both pointers traverse the same total length. Eventually, if the lists intersect, the pointers will meet at the intersection node. If not, both pointers will become null, ending the search. We compare pointers directly to check if they point to the same node in memory. This method runs in linear time and uses constant extra space.

Practice

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

Solution

  1. Step 1: Understand linked list nodes

    Each node in a linked list has a value and a reference (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, not just the same value.
  3. Final Answer:

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

    Intersection means shared node reference = A [OK]
Hint: Intersection means same node object, not just same value [OK]
Common Mistakes:
  • Confusing same value with same node reference
  • Thinking intersection is the last node always
  • Assuming intersection means equal length lists
2. Which of the following C++ code snippets correctly declares a singly linked list node structure for intersection detection?
easy
A. struct Node { int data; Node* next; };
B. struct Node { int data; Node next; };
C. struct Node { int data; Node* next(){}; };
D. struct Node { int data; Node next(); };

Solution

  1. Step 1: Understand pointer usage in C++ structs

    A linked list node must have a pointer to the next node, so Node* next; is correct.
  2. Step 2: Analyze options

    struct Node { int data; Node* next; }; uses Node* next; which is correct. Options A, B, and D misuse pointer or function syntax.
  3. Final Answer:

    struct Node { int data; Node* next; }; -> Option A
  4. Quick Check:

    Pointer to next node = C [OK]
Hint: Use pointer (*) for next node in struct [OK]
Common Mistakes:
  • Forgetting the * pointer for next node
  • Using function syntax instead of pointer
  • Declaring next as object instead of pointer
3. Given two linked lists intersecting at node with value 7, what will the following code print?
Node* getIntersectionNode(Node* headA, Node* headB) {
    Node* a = headA;
    Node* b = headB;
    while (a != b) {
        a = a ? a->next : headB;
        b = b ? b->next : headA;
    }
    return a;
}

// Lists:
// A: 3 -> 6 -> 9 -> 7 -> 10
// B: 1 -> 7 -> 10

Node* intersection = getIntersectionNode(headA, headB);
if (intersection) std::cout << intersection->data << std::endl;
else std::cout << -1 << std::endl;
medium
A. -1
B. 10
C. 7
D. 3

Solution

  1. Step 1: Understand the algorithm

    This code uses two pointers traversing both lists; when one reaches end, it switches to the other list's head. They meet at intersection.
  2. Step 2: Trace the pointers

    Both pointers meet at node with value 7, which is the intersection point.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    Intersection node data = 7 [OK]
Hint: Two-pointer switch method finds intersection node [OK]
Common Mistakes:
  • Assuming intersection is node with value 10
  • Printing -1 when intersection exists
  • Confusing node value with node reference
4. Identify the bug in this code snippet that tries to find the intersection point of two linked lists:
Node* getIntersection(Node* headA, Node* headB) {
    while (headA != headB) {
        headA = headA->next;
        headB = headB->next;
    }
    return headA;
}
medium
A. It does not handle different list lengths, causing null pointer errors.
B. It uses incorrect comparison operator for nodes.
C. It returns the wrong node after loop ends.
D. It should use recursion instead of iteration.

Solution

  1. Step 1: Analyze pointer movement

    The code moves both pointers forward simultaneously without adjusting for different list lengths.
  2. Step 2: Identify consequence

    If lists differ in length, one pointer may reach null before the other, causing a null pointer dereference or infinite loop.
  3. Final Answer:

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

    Unequal lengths cause errors = D [OK]
Hint: Adjust pointers for length difference before comparing [OK]
Common Mistakes:
  • Ignoring list length difference
  • Dereferencing null pointers
  • Assuming simultaneous traversal always works
5. You have two singly linked lists that intersect. List A has length 8, List B has length 5. Which approach correctly finds the intersection point efficiently?
hard
A. Traverse both lists fully and store nodes in hash sets, then find first common node.
B. Calculate length difference, advance longer list by difference, then move both pointers together.
C. Reverse both lists and compare nodes from start until they differ.
D. Compare node values one by one from heads until a matching value is found.

Solution

  1. Step 1: Understand length difference method

    Calculate difference (8 - 5 = 3), advance longer list by 3 nodes to align both lists.
  2. Step 2: Traverse both lists together

    Move both pointers one step at a time; the first node where they match is the intersection.
  3. Final Answer:

    Calculate length difference, advance longer list by difference, then move both pointers together. -> Option B
  4. Quick Check:

    Align by length difference then traverse = B [OK]
Hint: Advance longer list by length difference before simultaneous traversal [OK]
Common Mistakes:
  • Comparing node values instead of references
  • Reversing lists unnecessarily
  • Using extra space when not needed
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