Reverse Linked List
Reverse the directional arrows of a singly linked list in-place. The classic test of reference manipulation and memory pointer management.
Problem Statement
Given the head of a singly linked list, reverse the list, and return the reversed list.
Examples & Constraints
- The number of nodes in the list is the range [0, 5000].
- -5000 <= Node.val <= 5000
Select Approach (2 Solutions)
Iterative Pointer Reversal (Three Pointers)
Optimal ApproachTraverse the list while adjusting each node's next pointer to reference its previous predecessor using prev, curr, and nextTemp pointers.
Every node in the linked list is visited exactly once.
Only three pointer variables (prev, curr, nextTemp) are used. No new nodes are created.
Underlying algorithmic logic
The Core Mental Model: The Train Coupling Trap
In a singly linked list, nodes only hold a forward reference (curr.next).
Imagine a series of train cars: 1 -> 2 -> 3.
If you stand at car 2 and disconnect its forward link to point it back at car 1 (curr.next = prev), car 3 will decouple and drift away into memory. You lose the rest of your list forever!
To prevent memory leaks and lost nodes, you must follow the "Hold Before You Cut" rule:
Always store a temporary reference to the next node before overwriting the current node's next pointer.
The 4-Step Pointer Shuffle
At each node in the while (curr != null) loop:
- Hold the future:
nextTemp = curr.next(save the rest of the list). - Reverse the pointer:
curr.next = prev(point backwards). - Advance prev:
prev = curr(move previous pointer forward). - Advance curr:
curr = nextTemp(move current pointer forward).
When curr finally becomes null (walking off the end of the original list), prev will be pointing at the very last node, which is now the new head of our reversed list.
Step-by-step execution walkthrough
prev = null, curr = Node(1). Save nextTemp = Node(2).
curr.next points to prev (null). Advance: prev = Node(1), curr = Node(2).
curr.next points to Node(1). Advance: prev = Node(2), curr = Node(3).
Repeat until curr is null. Return prev as the new head.
- •Optimal O(n) execution
- •O(1) in-place memory usage
- •Cleanest and most robust solution
- •Destructive to the original list's forward pointers
Code Implementations
Ready-to-useCopy & paste production-ready solutions for Iterative Pointer Reversal (Three Pointers) in your language of choice
from __future__ import annotations# Definition for singly-linked list.class ListNode:def __init__(self, val: int = 0, next: ListNode | None = None):self.val = valself.next = nextdef reverse_list(head: ListNode | None) -> ListNode | None:prev = Nonecurr = headwhile curr:next_temp = curr.nextcurr.next = prevprev = currcurr = next_tempreturn prevdef print_list(head: ListNode | None) -> None:values = []curr = headwhile curr:values.append(str(curr.val))curr = curr.nextprint(" -> ".join(values))if __name__ == "__main__":head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5)))))print("Original list:")print_list(head)reversed_head = reverse_list(head)print("Reversed list:")print_list(reversed_head)
Understand Singly Linked List Visually
Interact with linear chain node arrays, traversal, head/tail adjustments, and index operations. Test live edge cases and watch memory state transitions step-by-step.
Frequently Asked Questions
Related Logic & Algorithm Problems
Valid Parentheses
Verify that code brackets (), {}, and [] are properly closed and nested in order. This is the exact algorithm compilers, linters, and IDEs use to detect syntax errors in real-time.
Two Sum
Find the two numbers in an array that sum up to a target value. The quintessential problem for learning how to trade memory for speed using a Hash Map.
Binary Search
Locate a target value in a sorted array in O(log n) logarithmic time. Like opening a dictionary in the middle, each comparison eliminates half the remaining items.