Bird
0
0
DSA Cprogramming~10 mins

Reverse a Singly Linked List Iterative in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Reverse a Singly Linked List Iterative
Start with head node
Initialize prev = NULL, current = head
While current != NULL
Save next = current->next
Reverse pointer: current->next = prev
Move prev = current
Move current = next
Repeat loop
Loop ends when current == NULL
Return prev as new head
We start from the head and move through the list, reversing each node's pointer to point backward, until we reach the end. The prev pointer becomes the new head.
Execution Sample
DSA C
Node* reverseList(Node* head) {
  Node* prev = NULL;
  Node* current = head;
  while (current != NULL) {
    Node* next = current->next;
    current->next = prev;
    prev = current;
    current = next;
  }
  return prev;
}
This code reverses a singly linked list by changing the next pointers of each node iteratively.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
0Initialize prev=NULL, current=head (1)1 -> 2 -> 3 -> NULLprev=NULL, current=11 -> 2 -> 3 -> NULL
1Save next = current->next (2)1 -> 2 -> 3 -> NULLnext=21 -> 2 -> 3 -> NULL
2Reverse current->next to prev (NULL)1 -> 2 -> 3 -> NULL1->next=NULL1 -> NULL 2 -> 3 -> NULL
3Move prev = current (1)1 -> 2 -> 3 -> NULLprev=11 -> NULL 2 -> 3 -> NULL
4Move current = next (2)1 -> 2 -> 3 -> NULLcurrent=21 -> NULL 2 -> 3 -> NULL
5Save next = current->next (3)1 -> NULL 2 -> 3 -> NULLnext=31 -> NULL 2 -> 3 -> NULL
6Reverse current->next to prev (1)1 -> NULL 2 -> 3 -> NULL2->next=12 -> 1 -> NULL 3 -> NULL
7Move prev = current (2)2 -> 1 -> NULL 3 -> NULLprev=22 -> 1 -> NULL 3 -> NULL
8Move current = next (3)2 -> 1 -> NULL 3 -> NULLcurrent=32 -> 1 -> NULL 3 -> NULL
9Save next = current->next (NULL)2 -> 1 -> NULL 3 -> NULLnext=NULL2 -> 1 -> NULL 3 -> NULL
10Reverse current->next to prev (2)2 -> 1 -> NULL 3 -> NULL3->next=23 -> 2 -> 1 -> NULL
11Move prev = current (3)3 -> 2 -> 1 -> NULLprev=33 -> 2 -> 1 -> NULL
12Move current = next (NULL)3 -> 2 -> 1 -> NULLcurrent=NULL3 -> 2 -> 1 -> NULL
13Loop ends, return prev as new head (3)3 -> 2 -> 1 -> NULLreturn prev3 -> 2 -> 1 -> NULL
💡 current becomes NULL at step 12, loop ends, prev points to new head
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
prevNULL123333
current123NULLNULLNULLNULL
nextundefined23NULLNULLNULLNULL
Key Moments - 3 Insights
Why do we need to save 'next' before reversing the pointer?
Because once we reverse current->next to prev, we lose the original next node. Saving 'next' first (see step 1 and 5) lets us continue traversing the list.
Why does the loop stop when current becomes NULL?
The loop condition is while(current != NULL). When current is NULL (step 12), it means we reached past the last node, so reversal is complete.
What does 'prev' point to at the end?
At the end (step 13), 'prev' points to the new head of the reversed list, which was the last node before reversal.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 6, what does node 2's next pointer point to?
ANULL
BNode 3
CNode 1
DNode 2 itself
💡 Hint
Check the 'Pointer Changes' column at step 6 where 2->next is set to 1.
At which step does the 'current' pointer become NULL?
AStep 12
BStep 10
CStep 13
DStep 9
💡 Hint
Look at the 'current' variable in variable_tracker and execution_table rows.
If we forget to update 'prev = current' inside the loop, what happens to the final list?
AList remains unchanged
BList is partially reversed or broken
CList becomes empty
DList is reversed correctly
💡 Hint
Refer to the importance of 'prev' updates in the execution_table steps 3, 7, and 11.
Concept Snapshot
Reverse a singly linked list iteratively:
- Use three pointers: prev (NULL), current (head), next (temp)
- Loop while current != NULL:
  * Save next = current->next
  * Reverse current->next = prev
  * Move prev = current
  * Move current = next
- Return prev as new head
This reverses links one by one, changing direction.
Full Transcript
This visualization shows how to reverse a singly linked list using an iterative method. We start with three pointers: prev set to NULL, current set to the head of the list, and next used to temporarily store the next node. In each step, we save the next node, reverse the current node's pointer to point to prev, then move prev and current forward. This continues until current becomes NULL, meaning we have processed all nodes. The prev pointer then points to the new head of the reversed list. The execution table tracks each step, showing how pointers change and how the list nodes rearrange visually. Key moments clarify why saving next is necessary before reversing, why the loop stops when current is NULL, and what prev points to at the end. The quiz tests understanding of pointer changes and loop termination. This method efficiently reverses the list in-place without extra memory.