How to Reverse a Linked List in JavaScript

A tutorial on reversing a linked list in JavaScript.

Cosmocoder
JavaScript in Plain English

--

This is a solution for Leetcode’s #206 and for me, this problem really gives a thorough understanding of the steps involved in linked list traversal.

When taking ‘steps’ in abstract space (along a linked list), it’s all about three central concerns:

  1. Where were you?
  2. Where are you now?
  3. Where are you going?

And it’s the storage and shuffling of this information that allows for a very simple solution to this problem.

Given our node class…

This was my approach:

I decided to go with a while loop as opposed to recursion on this one just to break it up and do something new.

Adequately tracking my previous, current, and next values allows me to break and create new ‘next’ pointers as needed while not losing any aspect of the list along the way.

And there you have it. Thank you for reading.

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

--

--

Hi. I’m Nathan and I’m a full-stack software dev making my way through DS and Algo. Looking forward to sharing experiences along the way.