Understanding Singly Linked List Reversal in JavaScript

When first learning data structures in JavaScript, the concept I struggled with the most was reversing a singly linked list. Even after reading a solution, I could make neither head nor tail (pun intended) of it. Throwing in console logs at various points in the code didn’t help me understand, because the console would just display [Object] without stating the values. Only by manually writing out the value of each variable during each step was I able to fully understand how the reversal works.

The iterative code and sample linked list I’m using here is taken from

Andrei Neagoie’s Udemy course on data structures & algorithms (highly recommend his courses). Here is the solution JavaScript code to reverse:

In array form, our simple example linked list looks like [1, 10, 16, 88]. In singly linked list form, it looks like:

The first two lines of solution code check if there is only one node in the linked list, and return the list if that is the case, since we don’t need to reverse a single item.

Next, we set “first” and “this.tail” to be references to this.head (see image above) with:

Our final line before we begin our while loop is let second = first.next, so:

With our variables defined, we begin our while loop, whose code again is:

Here are our values for the first loop:

The values of our second loop:

And finally, our 3rd loop:

While(second) is now null, so the loop stops. Did you notice the pattern each iteration of the loop? We take the second variable (originally this.head.next) and make its next variable equal to first (originally this.head). So we have the next variable followed by this.head, and we set first equal to all this. We increment each iteration by storing the second.next value in a temporary (temp) variable.

What we’re left with after looping finishes is a reversed linked list followed by the original linked list (this.head). We need to truncate the next portions of the original linked list, which we accomplish with the final lines of code:

Here I highlight with capital letters the this.head portion of first after looping is finished:

Throughout the looping, this.head was referenced but never altered. After the looping we set this.head.next equal to null, so now this.head is:

At the very beginning of the reverse() method, we set this.tail = this.head. So now this.tail points to the single node with value of 1 and next of null, shown above. After setting this.head.next to null, we made this.head point to first. Since this.head is now a reference to first, this.tail retains the value of this.head before it was set to a reference, so it stays the value shown above.

Here’s a simple example code in console.log() illustrating that a reference won’t update to a reference of a reference:

After all is said and done, with this.head.next = null, first is now:

this.head points to first, and thus our array has been reversed.

That was a lot to take in, and the numerous swapping around of references makes it easy to get lost. Writing out each variable at each step in a simple example is the easiest way to understand what happens when a singly linked list is reversed in JavaScript. If you need to, re-read the whole process, write it out yourself, and/or find other sources. Remember: you can always reset and come back to it later with fresh eyes and mind if the concepts just aren’t sticking.

Thanks for reading this far, and I hope this article was of some help!