# Reverse Linked List With JavaScript

In this article, I will be showing you how to solve the [LeetCode Reverse Linked List](https://leetcode.com/problems/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](https://cdn.hashnode.com/res/hashnode/image/upload/v1638600722125/C5ddMajVH.png)

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

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

```javascript
/**
 * 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:

```javascript
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:

```javascript
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:

```javascript
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:

```javascript
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](https://github.com/Blake-K-Yeboah)

**LinkedIn:** [Blake-K-Yeboah](https://www.linkedin.com/in/blake-yeboah/)

You can also show your support by buying me a coffee 😃
 
<a href="https://www.buymeacoffee.com/blakeyeboah"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=blakeyeboah&button_colour=855ff6&font_colour=ffffff&font_family=Cookie&outline_colour=ffffff&coffee_colour=FFDD00"></a>


