Bird
0
0
DSA Cprogramming~10 mins

Sort a Linked List Using Merge Sort in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Sort a Linked List Using Merge Sort
Start with head of list
Check if list has 0 or 1 node?
YesReturn head (already sorted)
No
Split list into two halves
Recursively sort left half
Merge two sorted halves
Return merged sorted list
The list is split into halves recursively until single nodes remain, then merged back in sorted order.
Execution Sample
DSA C
Node* mergeSort(Node* head) {
  if (!head || !head->next) return head;
  Node* mid = getMiddle(head);
  Node* right = mid->next;
  mid->next = NULL;
  Node* left = mergeSort(head);
  right = mergeSort(right);
  return merge(left, right);
}
This code splits the list, sorts each half recursively, and merges them back sorted.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with full list4 nodes: 4->2->1->3->NULLhead points to 44 -> 2 -> 1 -> 3 -> NULL
2Find middle node4 nodesmid points to 24 -> 2 -> 1 -> 3 -> NULL
3Split list at midLeft: 4->2->NULL, Right: 1->3->NULLmid->next = NULLLeft: 4 -> 2 -> NULL, Right: 1 -> 3 -> NULL
4Recursively sort left half2 nodes: 4->2->NULLhead points to 44 -> 2 -> NULL
5Find middle of left half2 nodesmid points to 44 -> 2 -> NULL
6Split left halfLeft: 4->NULL, Right: 2->NULLmid->next = NULLLeft: 4 -> NULL, Right: 2 -> NULL
7Sort left-left half1 node: 4->NULLhead points to 44 -> NULL
8Sort left-right half1 node: 2->NULLhead points to 22 -> NULL
9Merge left halves2 nodesmerge 4 and 22 -> 4 -> NULL
10Recursively sort right half2 nodes: 1->3->NULLhead points to 11 -> 3 -> NULL
11Find middle of right half2 nodesmid points to 11 -> 3 -> NULL
12Split right halfLeft: 1->NULL, Right: 3->NULLmid->next = NULLLeft: 1 -> NULL, Right: 3 -> NULL
13Sort right-left half1 node: 1->NULLhead points to 11 -> NULL
14Sort right-right half1 node: 3->NULLhead points to 33 -> NULL
15Merge right halves2 nodesmerge 1 and 31 -> 3 -> NULL
16Merge sorted halves4 nodesmerge 2->4 and 1->31 -> 2 -> 3 -> 4 -> NULL
17Return sorted list4 nodeshead points to 11 -> 2 -> 3 -> 4 -> NULL
18EndSorted list returnedNo pointer changes1 -> 2 -> 3 -> 4 -> NULL
💡 All nodes merged back in sorted order, recursion ends.
Variable Tracker
VariableStartAfter Step 3After Step 9After Step 15After Step 16Final
head4->2->1->3->NULLLeft:4->2->NULL, Right:1->3->NULLLeft sorted:2->4->NULLRight sorted:1->3->NULLMerged:1->2->3->4->NULL1->2->3->4->NULL
midpoints to 2points to 4 (left half)points to 4 (left half)points to 1 (right half)points to 1 (right half)N/A
leftN/A4->2->NULL2->4->NULL2->4->NULL2->4->NULL2->4->NULL
rightN/A1->3->NULL1->3->NULL1->3->NULL1->3->NULL1->3->NULL
Key Moments - 3 Insights
Why do we split the list by setting mid->next = NULL?
Setting mid->next = NULL breaks the list into two separate halves so recursive calls sort each half independently, as shown in step 3 and step 6.
Why does the recursion stop when the list has 0 or 1 node?
A list with 0 or 1 node is already sorted, so the function returns immediately without further splitting, as seen in steps 7 and 8.
How does merging two sorted halves maintain order?
During merge (steps 9, 15, 16), nodes are compared one by one and linked in ascending order, ensuring the merged list is sorted.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the visual state after step 9?
A2 -> 4 -> NULL
B1 -> 3 -> NULL
C4 -> 2 -> NULL
D1 -> 2 -> 3 -> 4 -> NULL
💡 Hint
Check the 'Visual State' column at step 9 in the execution table.
At which step does the list get fully merged and sorted?
AStep 9
BStep 16
CStep 15
DStep 3
💡 Hint
Look for the step where the full 4-node list is merged in the execution table.
If the original list had only one node, what would happen in the execution?
AThe function would split the list anyway.
BThe function would merge with an empty list.
CThe function would return the node immediately without splitting.
DThe function would cause an error.
💡 Hint
Refer to the base case condition in the code and steps 7 and 8.
Concept Snapshot
Merge Sort on Linked List:
- Base case: 0 or 1 node returns as sorted
- Find middle node to split list
- Recursively sort left and right halves
- Merge two sorted halves by comparing nodes
- Result is a fully sorted linked list
Full Transcript
This visualization shows how merge sort works on a linked list. We start with the full list and find the middle node to split it into two halves. Each half is recursively sorted until we reach lists with one node, which are already sorted. Then, we merge the sorted halves step by step, comparing nodes to maintain order. Pointer changes like setting mid->next to NULL split the list physically. The final merged list is sorted. Key points include why we split the list, when recursion stops, and how merging maintains order.