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.
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.
The three variables matter:
- '
currentpoints at the node being processed' - '
next_nodepreserves the rest of the list before you overwritecurrent.next' - '
previousbecomes 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.
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.
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, andnext_nodeto 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
prevandnexton each node. - The critical implementation rule is to save the next node before changing any pointer.
Related reading
- How can I sort a coordinate list for a rectangle counterclockwise?
- How can I sort a List alphabetically?
- How can I sort a stdmap first by value, then by key?
- How can I sort an STL map by value?
- How can I sort a list of dictionaries by a value of the dictionary in Python?
- How can I sort generic list DESC and ASC?
- How can I sort Map values by key in Java?
- How can I sort the values in a custom Keras / Tensorflow `Loss` Function?

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 courseTrack 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.