0
0
DSA Pythonprogramming

Reverse a Singly Linked List Iterative in DSA Python

Choose your learning style9 modes available
Mental Model
We change the direction of each link one by one so the list points backward instead of forward.
Analogy: Imagine a line of people holding hands forward. We ask each person to turn around and hold the hand of the person behind them instead.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null
Goal: Reverse the list so it becomes 3 -> 2 -> 1 -> null
Step 1: Initialize prev to null and curr to head (node 1)
prev = null, curr = 1 -> 2 -> 3 -> null
Why: We start with prev as null because the new tail will point to null
Step 2: Save next node (2), reverse curr's pointer to prev (null), move prev to curr (1), move curr to next (2)
1 -> null [prev]
2 -> 3 -> null [curr]
Why: We reverse the first link and move forward to continue reversing
Step 3: Save next node (3), reverse curr's pointer to prev (1), move prev to curr (2), move curr to next (3)
2 -> 1 -> null [prev]
3 -> null [curr]
Why: We reverse the second link and move forward
Step 4: Save next node (null), reverse curr's pointer to prev (2), move prev to curr (3), move curr to next (null)
3 -> 2 -> 1 -> null [prev]
curr = null
Why: We reverse the last link; curr is now null so we stop
Result:
3 -> 2 -> 1 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

def reverse_linked_list(head):
    prev = None
    curr = head
    while curr is not None:
        next_node = curr.next  # save next node
        curr.next = prev       # reverse pointer
        prev = curr            # move prev forward
        curr = next_node       # move curr forward
    return prev

def print_list(head):
    curr = head
    result = []
    while curr:
        result.append(str(curr.val))
        curr = curr.next
    print(' -> '.join(result) + ' -> null')

# Driver code
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)

print_list(head)
reversed_head = reverse_linked_list(head)
print_list(reversed_head)
while curr is not None:
loop until we reach the end of the list
next_node = curr.next # save next node
keep track of next node before changing pointer
curr.next = prev # reverse pointer
reverse current node's pointer to previous node
prev = curr # move prev forward
advance prev to current node
curr = next_node # move curr forward
advance curr to next node to continue
return prev
prev is new head after full reversal
OutputSuccess
1 -> 2 -> 3 -> null 3 -> 2 -> 1 -> null
Complexity Analysis
Time: O(n) because we visit each node once to reverse its pointer
Space: O(1) because we only use a few extra variables, no extra list
vs Alternative: Compared to recursive reversal which uses O(n) space on call stack, iterative uses constant space
Edge Cases
empty list (head is null)
function returns null immediately, no reversal needed
DSA Python
while curr is not None:
single node list
pointer reversal does nothing visible, returns same single node
DSA Python
while curr is not None:
When to Use This Pattern
When you need to reverse the order of a singly linked list, use the iterative reversal pattern by changing pointers one by one.
Common Mistakes
Mistake: Not saving the next node before reversing pointer causes loss of access to rest of list
Fix: Always save curr.next in a variable before changing curr.next
Mistake: Returning head instead of prev after reversal
Fix: Return prev because it points to new head after reversal
Summary
Reverses a singly linked list by changing each node's pointer to the previous node.
Use when you want to reverse the order of nodes in a singly linked list efficiently.
The key is to save the next node before reversing the pointer to avoid losing the rest of the list.