Reverse a linked list by preserving the next link

← Back to Blog & Articles

Linked lists show up in interviews less because they’re common in production code — most languages give you a perfectly good dynamic array — and more because they’re an excellent test of pointer discipline.

Every operation in this article can be done with O(1) extra space by rewiring next pointers instead of copying values into a new structure, and every operation is also easy to get subtly wrong: lose a reference to the wrong node at the wrong moment and you silently drop half the list, with no exception raised to tell you.

The signal for reaching into this article’s toolbox is straightforward: the problem hands you a singly (occasionally doubly) linked list and asks you to reverse it, reorder it, merge it with another list, or restructure it — and, explicitly or implicitly, rewards you for not allocating a second list to hold the answer. If the problem instead asks you to search, sort by an arbitrary key, or do repeated random access into the middle of the list, a linked list is usually the wrong data structure for the job in the first place, and no amount of pointer cleverness fixes that; converting to an array first, or acknowledging that a doubly linked list plus a hash map would serve better (as in the LRU cache design problem later in the book), is the more honest answer in that case.

The techniques in this article build directly on each other. Reversal is the atomic operation: once you can reverse a list (or a slice of one) without extra memory, you can extend it to reverse in fixed-size chunks, and once you can merge two sorted lists by rewiring rather than copying, you can compose reversal and merging to solve much more elaborate restructuring problems. The dummy-node trick — a placeholder node whose next points at the real head — appears in nearly solutions of this form, because it removes the special-casing that “the head of the list is about to change” would otherwise force into your code.

Continue with the book

Explore the full treatment, worked examples, and exercises in Algorithmic Edge.


Companion article for Algorithmic Edge, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top