Better Programming

Advice for programmers.

Follow publication

Reverse Reverse!... A Linked List?

For your next technical interview

Joanna Lin
Better Programming
Published in
4 min readSep 14, 2020

Graphic displaying linked list of 1 > 2 > 3> 4, with head set at 1, and tail at 4. Current node is 1.
Our singly linked list example. Photo by the author.

After studying linked lists, this very popular question pops up: How do you reverse a linked list? Not just that, but how do you do it in place? The first time I reviewed this question, I had a hard time wrapping my head around it, but it finally clicked!

Let’s get started.

Singly Linked List Review

If you’re not familiar with the basics of a singly linked list, check out my previous article.

The foundation of our singly linked list can be seen on GitHub.

Overview

Our linked list example will be 1 → 2 → 3 → 4.

The result we’re looking for is 1 ← 2 ←3 ←4.

The key thing to remember about a singly linked list is that each node only contains its value and a pointer to the next node. There is no pointer to the previous node. So, we’ll have to keep a variable that holds the reference to the previous node. We’ll also have to keep a variable that holds the reference to the next node.

I’m sure you’re wondering why since each node has a next pointer. As we loop through the list and change the current node’s next pointer value, we lose our next pointer in the iteration. As a result, we’ll have to keep track of the next node to know where we’re going.

Our method will be starting at the beginning of the list and changing the direction of the pointers as we move along the list.

Graphic displaying linked list of 1 > 2 > 3> 4, with head set at 1, and tail at 4. Current node is 1.
Our singly linked list example.

Iterative Setup

Since we’re starting at the beginning of the linked list, our current node is the head of the list. We don’t have a previous value yet, so we’ll set previous to null. Our next reference variable will also be declared but not defined yet.

When we reverse a list, we already know that the last item becomes the first and the first item becomes the last. Knowing this, we can start by swapping the head and tail values.

Graphic displaying linked list of 1 > 2 > 3> 4. Head and tail have been swapped, so head is 4, and tail is 1.
Head and tail nodes have been swapped.

Note: This code is a continuation of the class Singly Linked List from the gist linked above.

Looping Through the List

There are various ways that you can loop through the linked list: a for loop, a while loop, etc. For the sake of this explanation, I’ll go through it using a while loop.

The meat (and potatoes!) of this question occurs during our loop:

  1. As we go through the loop, we quickly save the current node’s next pointer into our variable. This is because once we change the next value, we sever ties with it! So we have to save it into our next variable in order to know where to move on to.
  2. Reverse reverse! We set the current node’s next pointer to the previous node. When we first start, previous is set to null. So the initial head value of 1 now points to null. This is where we are reversing the values!
Graphic displaying linked list of 1 > 2 > 3> 4. Current node is 1, pointing to previous node null.
We have saved our next value and set the current node’s pointer to the previous node (null at the beginning).

3. We’re done with reversing and can move on, so we set the previous node to the current node.

4. And then move the current node to the next node, which is saved under the variable next.

Graphic displaying linked list of 1 > 2 > 3> 4. Current node is 2, previous node is 1.
Shifting our previous and current nodes up one.

5. Aaand repeat! Check out the next few images to get a feel for what happens in the while loop as we continue.

Graphic displaying linked list of 1 > 2 > 3> 4. Current node is 2, pointing to previous node 1. Next node is 3.
Saved the next variable. Set the next pointer of the current variable to the previous node.
Graphic displaying linked list of 1 > 2 > 3> 4. Previous node set to 2. Current node set to 3.
Increment our previous and current variables.
Graphic displaying linked list of 1 > 2 > 3> 4, previous node set to 2. Current node set to 3.
Saved the next variable. Set the next pointer of the current variable to the previous node.
Increment our previous and current variables.

Notice that our while loop states that we are looping as long as there is a node. Once we get to the tail (final) node, the next variable will be set to the tail’s next property, which is null. The tail’s next property will be set to the previous node, and the previous node is updated to be the current node. The current node is updated to be null. This makes us hop out of the while loop.

Big O

Solving this iteratively results in a time complexity of O(N), where N is the number of nodes since we’re looping through all nodes in the list. The space complexity is O(1) since we’re doing this in place and not using an external data structure.

Check out the complete gist.

I hope that helped! Thanks for reading.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Joanna Lin
Joanna Lin

Written by Joanna Lin

Most likely to drink too many cups of coffee | Most likely to run across the street to pet your dog | Software Engineer | open to work!

No responses yet

Write a response