Easy
Reverse Linked List
👶 The 5-Year-Old Summary
You have a train where each car connects to the next. Reverse the train so the last car is now at the front and the arrows point the other way!
🎮 Interactive Visualizer
Click "Step" to reverse the list!
Original: 1 → 2 → 3 → null
🧠 The Brain Hack
Three pointers: prev, curr, next. Save next, reverse curr's pointer to prev, then move all three forward. Think of it as turning the arrows one by one!
💻 The Clean Solution
def reverse_list(head):
prev = None
curr = head
while curr:
next_temp = curr.next # Save next
curr.next = prev # Reverse the pointer
prev = curr # Move prev forward
curr = next_temp # Move curr forward
return prev
# Example:
# 1 -> 2 -> 3 -> None
# becomes:
# None <- 1 <- 2 <- 3
Time: O(n) - one pass
Space: O(1) - only pointers!