ToolMight LogoToolMight
EasyLinked ListPattern: Pointer ReversalPattern: Iterative In-Place

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

Example 1
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: Pointers reversed from 1->2->3->4->5 to 5->4->3->2->1.
Example 2
Input: head = [1, 2]
Output: [2, 1]
Explanation: 2-node list inverted.
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 Approach
Time:O(n)
Space:O(1)

Traverse the list while adjusting each node's next pointer to reference its previous predecessor using prev, curr, and nextTemp pointers.

Time ComplexityO(n)

Every node in the linked list is visited exactly once.

Space ComplexityO(1)

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:

  1. Hold the future: nextTemp = curr.next (save the rest of the list).
  2. Reverse the pointer: curr.next = prev (point backwards).
  3. Advance prev: prev = curr (move previous pointer forward).
  4. 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

1
Initial State

prev = null, curr = Node(1). Save nextTemp = Node(2).

prev: null | curr: 1 -> 2
2
Reverse First Pointer

curr.next points to prev (null). Advance: prev = Node(1), curr = Node(2).

1 -> null | curr: 2 -> 3
3
Reverse Second Pointer

curr.next points to Node(1). Advance: prev = Node(2), curr = Node(3).

2 -> 1 -> null | curr: 3
4
Completion

Repeat until curr is null. Return prev as the new head.

Reversed list: 5 -> 4 -> 3 -> 2 -> 1 -> null
Advantages
  • •Optimal O(n) execution
  • •O(1) in-place memory usage
  • •Cleanest and most robust solution
Trade-offs
  • •Destructive to the original list's forward pointers

Code Implementations

Copy & paste production-ready solutions for Iterative Pointer Reversal (Three Pointers) in your language of choice

File: solution.py
Python
from __future__ import annotations
# Definition for singly-linked list.
class ListNode:
def __init__(self, val: int = 0, next: ListNode | None = None):
self.val = val
self.next = next
def reverse_list(head: ListNode | None) -> ListNode | None:
prev = None
curr = head
while curr:
next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp
return prev
def print_list(head: ListNode | None) -> None:
values = []
curr = head
while curr:
values.append(str(curr.val))
curr = curr.next
print(" -> ".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)
Interactive visualizer available

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.

Launch Singly Linked List Visualizer

Frequently Asked Questions

Related Logic & Algorithm Problems

All problems