0
0
DSA Pythonprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Reverse a Singly Linked List Iterative
Start with head node
Initialize prev = None, current = head
While current is not None
Save next node: next_node = current.next
Reverse pointer: current.next = prev
Move prev to current
Move current to next_node
Repeat loop
When current is None, prev is new head
Return prev as reversed list head
We start from the head and move through the list, reversing each pointer one by one until we reach the end. The prev pointer tracks the reversed part.
Execution Sample
DSA Python
def reverse_linked_list(head):
    prev = None
    current = head
    while current:
        next_node = current.next
        current.next = prev
        prev = current
        current = next_node
    return prev
This code reverses a singly linked list by changing the next pointers iteratively.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initialize prev=None, current=head (1)1 -> 2 -> 3 -> 4 -> Noneprev=None, current=11 -> 2 -> 3 -> 4 -> None
2Save next_node = current.next (2)1 -> 2 -> 3 -> 4 -> Nonenext_node=21 -> 2 -> 3 -> 4 -> None
3Reverse current.next to prev (3)1 -> 2 -> 3 -> 4 -> None1.next=None1 -> None, 2 -> 3 -> 4 -> None
4Move prev to current (4)1 -> None, 2 -> 3 -> 4 -> Noneprev=11 -> None, 2 -> 3 -> 4 -> None
5Move current to next_node (5)1 -> None, 2 -> 3 -> 4 -> Nonecurrent=21 -> None, 2 -> 3 -> 4 -> None
6Save next_node = current.next (6)1 -> None, 2 -> 3 -> 4 -> Nonenext_node=31 -> None, 2 -> 3 -> 4 -> None
7Reverse current.next to prev (7)1 -> None, 2 -> 3 -> 4 -> None2.next=12 -> 1 -> None, 3 -> 4 -> None
8Move prev to current (8)2 -> 1 -> None, 3 -> 4 -> Noneprev=22 -> 1 -> None, 3 -> 4 -> None
9Move current to next_node (9)2 -> 1 -> None, 3 -> 4 -> Nonecurrent=32 -> 1 -> None, 3 -> 4 -> None
10Save next_node = current.next (10)2 -> 1 -> None, 3 -> 4 -> Nonenext_node=42 -> 1 -> None, 3 -> 4 -> None
11Reverse current.next to prev (11)2 -> 1 -> None, 3 -> 4 -> None3.next=23 -> 2 -> 1 -> None, 4 -> None
12Move prev to current (12)3 -> 2 -> 1 -> None, 4 -> Noneprev=33 -> 2 -> 1 -> None, 4 -> None
13Move current to next_node (13)3 -> 2 -> 1 -> None, 4 -> Nonecurrent=43 -> 2 -> 1 -> None, 4 -> None
14Save next_node = current.next (14)3 -> 2 -> 1 -> None, 4 -> Nonenext_node=None3 -> 2 -> 1 -> None, 4 -> None
15Reverse current.next to prev (15)3 -> 2 -> 1 -> None, 4 -> None4.next=34 -> 3 -> 2 -> 1 -> None
16Move prev to current (16)4 -> 3 -> 2 -> 1 -> Noneprev=44 -> 3 -> 2 -> 1 -> None
17Move current to next_node (17)4 -> 3 -> 2 -> 1 -> Nonecurrent=None4 -> 3 -> 2 -> 1 -> None
18Loop ends, return prev as new head (18)4 -> 3 -> 2 -> 1 -> Nonereturn prev=44 -> 3 -> 2 -> 1 -> None
💡 current becomes None at step 17, loop ends, prev points to new head
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
prevNone12344
current1234NoneNone
next_nodeNone234NoneNone
Key Moments - 3 Insights
Why do we set current.next = prev instead of prev.next = current?
Because we reverse the link from current node to previous node. Setting current.next = prev changes the direction of the pointer. prev.next = current would create a cycle. See execution_table rows 3, 7, 11, 15.
Why do we need to save next_node before reversing the pointer?
Because once we do current.next = prev, we lose the original next node. Saving next_node before reversing lets us continue traversal. See execution_table rows 2, 6, 10, 14.
What happens when current becomes None?
It means we reached the end of the original list. The prev pointer now points to the new head of the reversed list. The loop ends and we return prev. See execution_table rows 17 and 18.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what is the visual state of the list?
A2 -> 1 -> None, 3 -> 4 -> None
B3 -> 2 -> 1 -> None, 4 -> None
C1 -> None, 2 -> 3 -> 4 -> None
D4 -> 3 -> 2 -> 1 -> None
💡 Hint
Check the Visual State column at step 7 in execution_table
At which step does current become None, ending the loop?
AStep 18
BStep 16
CStep 17
DStep 15
💡 Hint
Look at Pointer Changes column for current value in execution_table
If we forget to update prev = current inside the loop, what happens to the reversed list?
AThe list remains unchanged
BThe reversed list will only contain the first node
CThe list is reversed correctly
DThe reversed list will be empty
💡 Hint
Check variable_tracker for prev changes and how it affects the Visual State
Concept Snapshot
Reverse a singly linked list iteratively:
- Use three pointers: prev=None, current=head, next_node
- Loop while current is not None:
  * Save next_node = current.next
  * Reverse current.next to prev
  * Move prev to current
  * Move current to next_node
- Return prev as new head
This reverses links one by one until list end.
Full Transcript
This concept shows how to reverse a singly linked list using an iterative method. We start with two pointers: prev set to None and current set to the head of the list. In each step, we save the next node to continue traversal after reversing the pointer. Then we reverse the current node's next pointer to point to prev, effectively flipping the link direction. We move prev to current and current to the saved next node. This repeats until current becomes None, meaning we reached the end. At that point, prev points to the new head of the reversed list, which we return. The execution table tracks each step, showing how nodes and pointers change, and the variable tracker shows how prev, current, and next_node update. Key moments clarify why we save next_node before reversing and why we set current.next to prev. The visual quiz tests understanding of list states at different steps and the importance of updating pointers correctly.