Reverse Linked List With JavaScript

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

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 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.
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
letto 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.
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 ๐