Reverse Linked List With JavaScript

Reverse Linked List With JavaScript

ยท

3 min read

In this article, I will be showing you how to solve the LeetCode Reverse Linked List problem using JavaScript. I'll explain each step of developing a solution to help you improve your problem solving skills.

Lets get right into it

The Problem

The Problem

The question is asking us to reverse a linked list. Fairly simple right?

The structure of a list node from LeetCode is as follows:

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */

So the function takes in one parameter - head, and then returns a new List Node with the linked list reversed.

The Solution

To start off, we will need two variables. One variable will be for storing the current list item we are on - lets call this current. The initial value will be the head since we will loop through the list starting with head.

The second variable will be used for storing the previous list node which will allow us to reverse the list - lets call this prev. The initial value for prev will be null since at the start there will be no previous node.

At this point, after declaring these variables our solution looks like this:

var reverseList = function(head) {
    let current = head;
    let prev = null;
};

Using let to define these variables is important since we will need to reassign them later on.

Next, we will use a while loop with the condition set to current so that it stops when there is no current value, meaning we are at the end of the list.

Establish the while loop:

var reverseList = function(head) {
    let current = head;
    let prev = null;
    while (current) {

    }
};

Within this loop, we are gonna reverse each node by setting the current node's next equal to prev, the previous node, and changing the current node to the original next value. This may sound confusing, the code makes it look more clear:

var reverseList = function(head) {
    let current = head;
    let prev = null;
    while (current) {
        let hold = current.next; // Hold original next value
        current.next = prev; // Set the next value to the previous which reverses the list
        prev = current; // Move previous value to current value (move through list)
        current = hold; // Set current node to original next value
    }
};

We have now reversed the list so to complete our solution, we just have to return the prev value:

var reverseList = function(head) {
    let current = head;
    let prev = null;
    while (current) {
        let hold = current.next; // Hold original next value
        current.next = prev; // Set the next value to the previous which reverses the list
        prev = current; // Move previous value to current value (move through list)
        current = hold; // Set current node to original next value
    }
    return prev
};

The reason why we are return prev and not current is because when we reach the end of the list, the current node will be set to null since the next of the last node will equal null. We don't want to return null but rather the head of our new, reversed list. Hence, we return prev.

Take Away

You've now learned how to reverse a linked list using the JavaScript programming language. Its a fairly simple solution that efficiently reverses the linked list.

๐Ÿ‘Œ Thanks for reading this article!

If you like what I do and would love to see more related content, follow me on my other social platforms:

GitHub: Blake-K-Yeboah

LinkedIn: Blake-K-Yeboah

You can also show your support by buying me a coffee ๐Ÿ˜ƒ

Did you find this article valuable?

Support Blake Yeboah by becoming a sponsor. Any amount is appreciated!

ย