Bird
0
0
DSA Cprogramming~10 mins

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

Choose your learning style9 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.