Reverse a singly linked list

Given a reference to the head of a singly linked list, reverse it and return a reference to the head of the reversed list.

Mihai Bojin
Nerd For Tech
4 min readJul 27, 2021

--

Photo: Chain links
“Photo by Edge2Edge Media on Unsplash

🔔 This article was originally posted on my site, MihaiBojin.com. 🔔

Given a reference to the head of a singly linked list, reverse it and return a reference to the head of the reversed list.

This is a simple problem that can be solved in a few ways. In this article, I will solve it in the most elegant way I know — in-place, in linear time.

Note: if you’re not familiar with this data structure, see the Wikipedia article on Linked Lists.

A simple LinkedList implementation

But first, let’s start with a simple singly linked list implementation.

The code above represents a very simple linked list implementation that forms the foundation of our problem.

Before we proceed, we can make it a bit nicer. First, we need to retrieve all of the list’s elements, in order.

The Java idiomatic way is to implement an Iterator.

Another nicety is to override the toString method so that we can print out the list's elements:

Of course, before we proceed, let’s ensure our custom linked list implementation works as expected.

For the examples below, I used JUnit 5 and Hamcrest, but you can substitute them as you want.

Solving the puzzle

One easy way to solve this problem is to iterate over all the list’s elements, add them to a stack, and then pop them. This approach works, but it requires O(N) extra space.

If you’d be trying to reverse a list with millions of elements, you might run out of heap space.

We can do better!

As with most linked list algorithms, you can rely on two cursors:

  • the first one keeps a reference to the last element in the reversed list
  • the second one keeps a reference to the new head of the original list

Step by step, we will iterate over the original list, constructing it as we go along. In the end, we will be left with a reference to the head of the reversed list.

Let’s see this in action!

Of course, we also need a test to confirm the solution works:

And there you have it: in-place (no extra space), linear (iterate over the input only once), singly linked list reversal.

I hope you enjoyed this exercise; it is a more elegant solution than its simpler alternative (using a stack).

Understanding the dual pointer concept is the key to efficiently solving linked list coding puzzles. Therefore, I recommend spending a bit of time studying it, as it will surely come in handy!

If you have any questions or seek any advice, please (DM me on Twitter)!

Until next time…

If you liked this article and want to read more like it, please subscribe to my newsletter; I send one out every few weeks!

--

--

Mihai Bojin
Nerd For Tech

Software Engineer at heart, Manager by day, Indie Hacker at night. Writing about DevOps, Software engineering, and Cloud computing. Opinions my own.