Linked List
Data Structures
Reverse Linked List
Algorithms
Coding Tutorial

How can I reverse a linked list?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Reversing a linked list is a classic pointer-manipulation problem because it looks simple but forces you to update references in exactly the right order. The standard in-place solution runs in linear time and constant extra space, which is why it appears so often in interviews and systems code. Once you understand the pointer movement, the algorithm becomes mechanical.

Reversing a Singly Linked List Iteratively

In a singly linked list, each node points only to the next node. To reverse the list, walk through it once and redirect each next pointer toward the previous node.

python
1class Node:
2    def __init__(self, value, next_node=None):
3        self.value = value
4        self.next = next_node
5
6
7def reverse_linked_list(head):
8    previous = None
9    current = head
10
11    while current is not None:
12        next_node = current.next
13        current.next = previous
14        previous = current
15        current = next_node
16
17    return previous
18
19
20def print_list(head):
21    values = []
22    current = head
23    while current is not None:
24        values.append(str(current.value))
25        current = current.next
26    print(" -> ".join(values))
27
28head = Node(1, Node(2, Node(3, Node(4))))
29print_list(head)
30reversed_head = reverse_linked_list(head)
31print_list(reversed_head)

The three variables matter:

  • 'current points at the node being processed'
  • 'next_node preserves the rest of the list before you overwrite current.next'
  • 'previous becomes the new next pointer'

If you forget to save next_node first, you lose access to the remaining list.

Why the Algorithm Works

At each step, one node moves from the unreversed portion to the reversed portion. Initially, the reversed portion is empty, so previous starts as None. After the first iteration, the old head points to None, which is exactly what the new tail should do.

That process continues until current becomes None. At that moment, previous is the new head of the reversed list.

This algorithm has:

  • time complexity O(n)
  • extra space complexity O(1)

Those are the best practical bounds for reversing a list in place.

A Recursive Version

Recursion is shorter conceptually, although it uses call-stack space.

python
1class Node:
2    def __init__(self, value, next_node=None):
3        self.value = value
4        self.next = next_node
5
6
7def reverse_recursive(head):
8    if head is None or head.next is None:
9        return head
10
11    new_head = reverse_recursive(head.next)
12    head.next.next = head
13    head.next = None
14    return new_head

This works by reversing the rest of the list first, then hanging the current node off the back. It is elegant, but the iterative solution is usually preferred in production because it avoids recursion depth limits and stack overhead.

Reversing a Doubly Linked List

A doubly linked list stores both next and prev. Reversal becomes a swap of those pointers on each node.

python
1class DoubleNode:
2    def __init__(self, value, prev_node=None, next_node=None):
3        self.value = value
4        self.prev = prev_node
5        self.next = next_node
6
7
8def reverse_doubly_linked_list(head):
9    current = head
10    new_head = None
11
12    while current is not None:
13        current.prev, current.next = current.next, current.prev
14        new_head = current
15        current = current.prev
16
17    return new_head

The traversal direction changes after the swap, which is why the loop advances with current = current.prev.

Common Pitfalls

The most common mistake is overwriting current.next before saving the original next node. That disconnects the remaining list and leaves you with partial data.

Another mistake is forgetting edge cases. An empty list and a single-node list should both return immediately without error. The iterative algorithm already handles both cases if written carefully.

In recursive solutions, developers often forget to set head.next = None after re-linking. If you omit that line, the old pointers can create a cycle.

Finally, do not confuse reversing the nodes with reversing only the values. Swapping node values may satisfy a toy exercise, but it is not the same algorithm and does not teach the actual pointer manipulation problem.

Summary

  • The standard iterative solution uses previous, current, and next_node to reverse pointers safely.
  • Reversing a singly linked list in place takes linear time and constant extra space.
  • A recursive version exists, but it uses additional call-stack space.
  • Doubly linked lists can be reversed by swapping prev and next on each node.
  • The critical implementation rule is to save the next node before changing any pointer.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.