Bird
Raised Fist0
DSA Cprogramming~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 at head of List1 and List2
↓
Calculate lengths of both lists
↓
Align start pointers by skipping extra nodes in longer list
↓
Traverse both lists together
↓
Check if nodes are same (by reference)
↓
Intersection
↓
End if no intersection
Find lengths, align starts, then move both pointers step-by-step to find the first common node.
Execution Sample
DSA C
Node* getIntersection(Node* head1, Node* head2) {
  int len1 = length(head1);
  int len2 = length(head2);
  while (len1 > len2) { head1 = head1->next; len1--; }
  while (len2 > len1) { head2 = head2->next; len2--; }
  while (head1 && head2) {
    if (head1 == head2) return head1;
    head1 = head1->next;
    head2 = head2->next;
  }
  return NULL;
}
This code finds the intersection node of two linked lists by aligning their starts and moving together.
Execution Table
StepOperationList1 PointerList2 PointerActionVisual State
1Calculate length of List1head1 at Node Ahead2 at Node XLength1 = 5List1: A->B->C->D->E->NULL List2: X->Y->C->D->E->NULL
2Calculate length of List2head1 at Node Ahead2 at Node XLength2 = 5Same as above
3Align pointershead1 at Node Ahead2 at Node XLengths equal, no skipPointers unchanged
4Compare nodesNode ANode XNot same, move bothPointers move to B and Y
5Compare nodesNode BNode YNot same, move bothPointers move to C and C
6Compare nodesNode CNode CSame node foundIntersection at Node C
7Return intersectionNode CNode CReturn Node CEnd
💡 Intersection found at Node C, pointers matched by reference.
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5After Step 6Final
head1Node ANode ANode BNode CNode CNode C
head2Node XNode XNode YNode CNode CNode C
len1055555
len2055555
Key Moments - 3 Insights
Why do we skip nodes in the longer list before comparing?
Because the lists may have different lengths, skipping extra nodes aligns the pointers so they have equal nodes left to traverse, ensuring correct comparison (see Step 3 in execution_table).
Why do we compare nodes by reference, not by value?
Intersection means the exact same node in memory, not just same value. So we check if pointers are equal (Step 6), not if data matches.
What if there is no intersection?
Both pointers will reach NULL without matching nodes, and the function returns NULL (not shown here but implied after Step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the position of head1 after Step 4?
ANode B
BNode C
CNode A
DNode Y
💡 Hint
Check the 'List1 Pointer' column at Step 4 in execution_table.
At which step do head1 and head2 point to the same node?
AStep 5
BStep 6
CStep 4
DStep 7
💡 Hint
Look for 'Same node found' in the Action column of execution_table.
If List1 was longer by 2 nodes, what would change in the execution_table?
ASteps 4 and 5 would be skipped
BIntersection would be found earlier
CSteps 3 would show skipping 2 nodes in List1
DLengths would be equal
💡 Hint
Refer to Step 3 where pointer alignment happens by skipping extra nodes.
Concept Snapshot
Intersection Point of Two Linked Lists:
- Find lengths of both lists
- Skip extra nodes in longer list to align
- Move both pointers step-by-step
- Check if pointers are same (by reference)
- Return intersection node or NULL if none
Full Transcript
To find the intersection point of two linked lists, first calculate the lengths of both lists. Then, skip the extra nodes in the longer list so both pointers start equally far from the end. Next, move both pointers forward one node at a time, comparing if they point to the same node by reference. If yes, that node is the intersection. If no intersection exists, both pointers reach NULL and the function returns NULL.

Practice

(1/5)
1. What does the term intersection point of two linked lists mean?
easy
A. The node where both linked lists have the same value but different references
B. The node where both linked lists share the same reference
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 and references

    Each node in a linked list has a reference (address) to the next node. Two lists intersect if they share the same node reference, not just the same value.
  2. Step 2: Define intersection point

    The intersection point is the first node where both lists point to the exact same node object in memory.
  3. Final Answer:

    The node where both linked lists share the same reference -> Option B
  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 based on node values
  • Assuming intersection is at list ends
2. Which of the following C code snippets correctly declares a node structure for a singly linked list used to find intersection?
easy
A. struct Node { int data; struct Node next; };
B. struct Node { int data; Node* next; };
C. typedef struct { int data; struct Node* next; } Node;
D. typedef struct Node { int data; struct Node* next; } Node;

Solution

  1. Step 1: Understand correct struct declaration

    In C, to declare a linked list node, you define a struct with data and a pointer to the same struct type.
  2. Step 2: Check each option

    typedef struct Node { int data; struct Node* next; } Node; correctly uses typedef and struct Node with a pointer to struct Node. struct Node { int data; Node* next; }; misses typedef and uses Node* without struct keyword. typedef struct { int data; struct Node* next; } Node; misses struct name. struct Node { int data; struct Node next; }; incorrectly uses struct Node next (not a pointer).
  3. Final Answer:

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

    Correct struct with pointer to same struct [OK]
Hint: Use 'struct Node* next;' inside struct for pointer to next node [OK]
Common Mistakes:
  • Forgetting 'struct' keyword before pointer type
  • Using struct variable instead of pointer for next
  • Missing typedef or struct name
3. Given the following code snippet to find the intersection point of two linked lists, what will be the output?
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;
}

// Lists:
// List A: 1 -> 2 -> 3 \
//                      6 -> 7 -> NULL
// List B:       4 -> 5 /

// Intersection at node with value 6
Node* result = getIntersectionNode(headA, headB);
printf("%d", result->data);
medium
A. 3
B. 5
C. 6
D. NULL

Solution

  1. Step 1: Understand the two-pointer approach

    Two pointers traverse each list; when reaching end, switch to other list head. They meet at intersection or NULL if none.
  2. Step 2: Trace pointers on given lists

    Lists intersect at node with value 6. Pointers will meet there after switching lists.
  3. Final Answer:

    6 -> Option C
  4. Quick Check:

    Two-pointer meet at intersection node value 6 [OK]
Hint: Two pointers switch lists to align lengths and meet at intersection [OK]
Common Mistakes:
  • Assuming pointers meet at first node
  • Not switching pointers to other list head
  • Confusing node value with pointer address
4. Identify the bug in the following code snippet intended to find the intersection point of two linked lists:
Node* getIntersectionNode(Node* headA, Node* headB) {
    Node *a = headA, *b = headB;
    while (a != b) {
        if (a == NULL || b == NULL) {
            a = headB;
            b = headA;
        } else {
            a = a->next;
            b = b->next;
        }
    }
    return a;
}
medium
A. The loop may run infinitely if no intersection exists
B. Pointers should not switch lists when NULL
C. The function does not handle empty lists
D. The comparison should be 'a->data == b->data' instead of 'a != b'

Solution

  1. Step 1: Analyze loop termination condition

    The loop ends when pointers a and b are equal (same node or NULL).
  2. Step 2: Check pointer switching logic

    When a or b is NULL, they switch to other list head. But if no intersection, pointers cycle indefinitely without becoming equal.
  3. Final Answer:

    The loop may run infinitely if no intersection exists -> Option A
  4. Quick Check:

    Loop lacks proper NULL check, causing infinite loop [OK]
Hint: Add explicit NULL check to break loop if no intersection [OK]
Common Mistakes:
  • Assuming pointers always meet
  • Comparing node data instead of node pointers
  • Not handling empty list inputs
5. You have two singly linked lists that may or may not intersect. Write a function to find the intersection point efficiently. Which approach below is best and why? A) Use two pointers starting at heads, switch to other list when reaching end, until they meet. B) Use nested loops to compare every node of list A with every node of list B. C) Store all nodes of list A in a hash set, then check nodes of list B for presence. D) Reverse both lists and compare nodes from the start until they differ.
hard
A. Two-pointer switching approach; O(n) time, O(1) space
B. Nested loops; O(n*m) time, O(1) space
C. Hash set approach; O(n+m) time, O(n) space
D. Reverse lists; modifies input and O(n) time

Solution

  1. Step 1: Evaluate time and space complexity of each approach

    Two-pointer switching approach; O(n) time, O(1) space uses two pointers switching lists, runs in linear time and constant space. Nested loops; O(n*m) time, O(1) space is nested loops, very slow. Hash set approach; O(n+m) time, O(n) space uses extra space for hash set. Reverse lists; modifies input and O(n) time modifies lists, which is not ideal.
  2. Step 2: Choose best approach for efficiency and safety

    Two-pointer switching approach; O(n) time, O(1) space is best because it is efficient (O(n)) and does not use extra space or modify lists.
  3. Final Answer:

    Two-pointer switching approach; O(n) time, O(1) space -> Option A
  4. Quick Check:

    Two-pointer method is optimal for intersection detection [OK]
Hint: Two-pointer switch method is fastest and uses no extra space [OK]
Common Mistakes:
  • Choosing nested loops causing slow performance
  • Using extra memory unnecessarily
  • Modifying input lists causing side effects
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