Reverse a Singly Linked List Iterative in DSA Python - Time & Space Complexity
We want to understand how the time needed to reverse a singly linked list changes as the list gets bigger.
Specifically, how does the number of steps grow when we reverse the list one node at a time?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, val):
self.val = val
self.next = None
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 direction of the links one by one until the entire list is reversed.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that visits each node once.
- How many times: Exactly once per node, so n times for n nodes.
As the list gets longer, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase in a straight line as the list size grows.
Time Complexity: O(n)
This means the time to reverse the list grows directly in proportion to the number of nodes.
[X] Wrong: "Reversing the list takes constant time because we just change pointers."
[OK] Correct: Even though each pointer change is quick, we must do it for every node, so the total time grows with the list size.
Understanding this time complexity helps you explain why reversing a list is efficient and what to expect as input grows, a useful skill in many coding challenges.
"What if we used recursion instead of iteration to reverse the list? How would the time complexity change?"
Practice
Solution
Step 1: Understand the linked list structure
A singly linked list has nodes pointing only to the next node, so reversing means changing these pointers to point backwards.Step 2: Purpose of iterative reversal
The iterative method changes each node's next pointer to the previous node, effectively reversing the list order.Final Answer:
To change the direction of all links so the list order is reversed -> Option CQuick Check:
Reversing means flipping links direction [OK]
- Confusing reversal with sorting
- Thinking reversal deletes nodes
- Assuming reversal finds middle node
Solution
Step 1: Understand pointer roles
Prev starts as null because the new tail will point to null; current starts at head to traverse the list.Step 2: Correct initialization
Setting prev = null and current = head allows iteration from the start with no previous node initially.Final Answer:
prev = null; current = head -> Option BQuick Check:
Prev null, current at head to start [OK]
- Setting prev to head initially
- Starting current as null
- Both pointers as null
Solution
Step 1: Trace reversal process
Initially: prev=null, current=1. After first iteration, 1 points to null. Then 2 points to 1, then 3 points to 2.Step 2: Final reversed list
The list becomes 3 -> 2 -> 1 -> null after all nodes reverse their next pointers.Final Answer:
3 -> 2 -> 1 -> null -> Option AQuick Check:
Reversed list order is 3, 2, 1 [OK]
- Not updating pointers correctly
- Confusing original and reversed order
- Including null as a node
def reverse(head):
prev = head
current = null
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
return prevSolution
Step 1: Check pointer initialization
Prev should be null initially because the new tail points to null; current should start at head to traverse.Step 2: Loop condition and pointer updates
Loop runs while current is not null; current.next is set to prev correctly; returning prev gives new head.Final Answer:
prev should start as null, current as head -> Option AQuick Check:
Prev null, current head to start loop [OK]
- Starting prev at head causes cycle
- Loop condition incorrect
- Returning wrong pointer
Solution
Step 1: Reverse first node pointer
Initially, prev=null, current=10. After first iteration, 10.next = null, so list is 10 -> null.Step 2: Reverse second node pointer
Next, current=20. Set 20.next = prev (which is 10), so list is 20 -> 10 -> null.Final Answer:
After first node: 10 -> null; After second node: 20 -> 10 -> null -> Option DQuick Check:
Each node points back to previous [OK]
- Not setting first node's next to null
- Linking nodes forward instead of backward
- Confusing order of pointer updates
Ready to prove it?
Pass the test and this pattern moves unknown
