“Reverse Linked List” Javascript solution Cheat Sheet

The “Reverse Linked List” is in the easy category and is a good start for understanding more challenging algorithms using a linked list as a data structure. I will focus on explaining a solution that works and not on the O time and space complexity.

Challenge: “Given the head of a singly linked list, reverse the list, and return the reversed list.”LeetCode

Note:

Example:

Explanation:

Create variables to keep track of reassigned values and iterate until we reach the end of the linked list.

1.Create variables to keep track

let currentNode = head;
let previousNode = null;
let nextNode = null;

2.Iterate until the end of the linked list when the head is equal to null

3.Reassign nodes values

nextNode = currentNode.next;
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;

4.Return reversed list link

return previousNode;

Solution :

--

--

Lifelong learner , Full Stack Software Engineer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store