Bird
0
0
DSA Cprogramming~10 mins

Reorder Linked List in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Reorder Linked List
Start at head
Find middle node
Split list into two halves
Reverse second half
Merge two halves alternately
Reordered list done
The list is split into two halves at the middle, the second half is reversed, then both halves are merged by alternating nodes.
Execution Sample
DSA C
void reorderList(Node* head) {
  Node* mid = findMiddle(head);
  Node* second = reverseList(mid->next);
  mid->next = NULL;
  mergeLists(head, second);
}
This code finds the middle of the list, reverses the second half, cuts the list, and merges the two halves alternately.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with full list1->2->3->4->5->NULLhead points to 11 -> 2 -> 3 -> 4 -> 5 -> NULL
2Find middle node1->2->3->4->5->NULLmid points to 31 -> 2 -> 3(mid) -> 4 -> 5 -> NULL
3Split list at middleFirst half: 1->2->3->NULL Second half: 4->5->NULLmid->next set to NULL1 -> 2 -> 3 -> NULL 4 -> 5 -> NULL
4Reverse second halfSecond half reversed: 5->4->NULLsecond points to 51 -> 2 -> 3 -> NULL 5 -> 4 -> NULL
5Merge two halves alternatelyMerged list: 1->5->2->4->3->NULLPointers rearranged to alternate nodes1 -> 5 -> 2 -> 4 -> 3 -> NULL
6EndReordered list completeNo pointer changes1 -> 5 -> 2 -> 4 -> 3 -> NULL
💡 All nodes merged alternately, list reordered successfully.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
headpoints to 1points to 1points to 1points to 1points to 1points to 1
midNULLpoints to 3points to 3points to 3points to 3points to 3
secondNULLNULLpoints to 4points to 5points to 5points to 5
list size5 nodes5 nodes3 nodes + 2 nodes3 nodes + 2 nodes5 nodes merged5 nodes merged
Key Moments - 3 Insights
Why do we split the list at the middle node and set mid->next to NULL?
Splitting at the middle breaks the list into two separate halves so we can reverse the second half independently. Setting mid->next to NULL ends the first half properly, preventing cycles during merging. See execution_table step 3.
Why do we reverse the second half before merging?
Reversing the second half allows us to merge nodes from the end of the original list alternately with nodes from the start, achieving the reorder pattern. See execution_table step 4.
How does merging alternate nodes work without losing any nodes?
We take one node from the first half, then one from the reversed second half, adjusting pointers carefully to link them alternately. This continues until all nodes are merged. See execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, which node does 'mid' point to?
ANode with value 2
BNode with value 3
CNode with value 4
DNode with value 5
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 2.
At which step does the second half of the list get reversed?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look for the operation 'Reverse second half' in the execution_table.
If we did not set mid->next to NULL at step 3, what would happen during merging?
AThe list would become cyclic or have incorrect links.
BThe second half would not reverse properly.
CThe list would merge correctly without issues.
DThe middle node would be lost.
💡 Hint
Refer to the explanation in key_moments about splitting the list and pointer changes at step 3.
Concept Snapshot
Reorder Linked List:
1. Find middle node using slow/fast pointers.
2. Split list into two halves at middle.
3. Reverse second half.
4. Merge halves alternately.
Result: nodes reordered as first, last, second, second last, etc.
Full Transcript
To reorder a linked list, we first find the middle node to split the list into two halves. We then reverse the second half so that we can merge nodes from the start and end alternately. After splitting, we set the middle node's next pointer to NULL to separate the halves. We then merge the two halves by alternating nodes from each half, adjusting pointers carefully. This process results in a reordered list where nodes appear in the order of first, last, second, second last, and so on.