Bird
0
0
DSA Cprogramming~10 mins

Merge Two Sorted Linked Lists in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Merge Two Sorted Linked Lists
Create dummy node
Set current pointer to dummy
Compare heads of both lists
Smaller node
Move current and chosen list pointer forward
Repeat comparison until one list ends
Attach remaining nodes of non-empty list
Return dummy.next as merged list head
We create a dummy node to start the merged list, then repeatedly pick the smaller node from the heads of the two lists, attach it, and move pointers until one list is empty. Finally, attach the leftover nodes.
Execution Sample
DSA C
ListNode* merge(ListNode* l1, ListNode* l2) {
  ListNode dummy = {0, NULL};
  ListNode* current = &dummy;
  while (l1 && l2) {
    if (l1->val < l2->val) {
      current->next = l1; l1 = l1->next;
    } else {
      current->next = l2; l2 = l2->next;
    }
    current = current->next;
  }
  current->next = l1 ? l1 : l2;
  return dummy.next;
}
This code merges two sorted linked lists by choosing the smaller head node each time and linking nodes to form a new sorted list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create dummy node and set current to dummydummy(0) -> NULLcurrent = dummydummy(0) -> NULL
2Compare l1(1) and l2(2), attach l1 to current.nextdummy(0) -> 1 -> NULLcurrent = 1, l1 moves to 3dummy(0) -> 1 -> NULL
3Compare l1(3) and l2(2), attach l2 to current.nextdummy(0) -> 1 -> 2 -> NULLcurrent = 2, l2 moves to 4dummy(0) -> 1 -> 2 -> NULL
4Compare l1(3) and l2(4), attach l1 to current.nextdummy(0) -> 1 -> 2 -> 3 -> NULLcurrent = 3, l1 moves to 5dummy(0) -> 1 -> 2 -> 3 -> NULL
5Compare l1(5) and l2(4), attach l2 to current.nextdummy(0) -> 1 -> 2 -> 3 -> 4 -> NULLcurrent = 4, l2 moves to NULLdummy(0) -> 1 -> 2 -> 3 -> 4 -> NULL
6l2 is NULL, attach remaining l1(5)dummy(0) -> 1 -> 2 -> 3 -> 4 -> 5 -> NULLcurrent = 5, l1 moves to NULLdummy(0) -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL
7Return dummy.next as merged list head1 -> 2 -> 3 -> 4 -> 5 -> NULLNo pointer changes1 -> 2 -> 3 -> 4 -> 5 -> NULL
💡 Loop ends when one list is empty; remaining nodes attached to merged list.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
l11 -> 3 -> 5 -> NULL3 -> 5 -> NULL3 -> 5 -> NULL5 -> NULL5 -> NULLNULLNULL
l22 -> 4 -> NULL2 -> 4 -> NULL4 -> NULL4 -> NULLNULLNULLNULL
currentdummy(0)123455
merged listemptydummy(0) -> 1 -> NULLdummy(0) -> 1 -> 2 -> NULLdummy(0) -> 1 -> 2 -> 3 -> NULLdummy(0) -> 1 -> 2 -> 3 -> 4 -> NULLdummy(0) -> 1 -> 2 -> 3 -> 4 -> 5 -> NULL1 -> 2 -> 3 -> 4 -> 5 -> NULL
Key Moments - 3 Insights
Why do we use a dummy node instead of starting directly with l1 or l2?
Using a dummy node simplifies linking nodes because it avoids special cases for the head. As seen in step 1 and 7, dummy.next points to the merged list head, so we always have a stable starting point.
What happens when one list becomes empty before the other?
When either l1 or l2 becomes NULL (step 5), we attach the remaining nodes of the other list directly to current->next. This ensures no nodes are lost and the merged list remains sorted.
Why do we move the current pointer after attaching a node?
Moving current forward (steps 2-5) keeps track of the last node in the merged list, so the next chosen node can be linked correctly. Without moving current, nodes would overwrite the same next pointer.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4. Which node does current point to after attaching?
ANode with value 2
BNode with value 3
CNode with value 4
DDummy node
💡 Hint
Check the 'Pointer Changes' column at step 4 in the execution_table.
At which step does the loop end because one list becomes empty?
AStep 7
BStep 6
CStep 5
DStep 3
💡 Hint
Look for when l2 moves to NULL in the variable_tracker and execution_table.
If l1 started with values all greater than l2, how would the merged list start?
AWith nodes from l2 first
BAlternating nodes from l1 and l2
CWith nodes from l1 first
DEmpty list
💡 Hint
Recall the comparison step picks the smaller node each time, as shown in the concept_flow.
Concept Snapshot
Merge Two Sorted Linked Lists:
- Use a dummy node to start merged list
- Compare heads of both lists
- Attach smaller node to current->next
- Move current and chosen list pointer forward
- Repeat until one list ends
- Attach remaining nodes
- Return dummy.next as merged list head
Full Transcript
To merge two sorted linked lists, we start by creating a dummy node to simplify linking. We use a pointer 'current' starting at dummy. Then, we compare the values at the heads of both lists. The smaller node is attached to current->next, and we move current and that list's pointer forward. We repeat this until one list is empty. After that, we attach the remaining nodes of the other list to current->next. Finally, we return dummy.next, which points to the head of the merged sorted list. This approach avoids special cases and ensures all nodes are included in order.