JavaScript Challenge - Reverse a Singly-Linked List

This community-built FAQ covers the “Reverse a Singly-Linked List” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the JavaScript challenge Reverse a Singly-Linked List

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello everyone, :smiley:

I resolved that challenge and want to share my answer with you :

function reverseLinkedList(linkedList) {
//linkedList argument represents the head of the Linked List

let prevPointer = null
let currentPointer = linkedList
let nextPointer = null

while (currentPointer !== null) {

//the pointer that should come after the current pointer now comes before the current pointer
prevPointer = currentPointer.next
//the first pointer will be the last so the next pointer of the current pointer should be null for the first iteration
currentPointer.next = nextPointer
//the pointer that will be the next pointer must be the current pointer
nextPointer = currentPointer
//the current pointer should now be the previous pointer of the initial current pointer
currentPointer = prevPointer

}
return nextPointer
}

Enjoy coding everyone ! :jigsaw: :wink:

3 Likes

I was a bit lazy and just put all the nodes in the list into an array,
and then used the indices in the array to switch the .next

function reverseLinkedList(linkedList) {
  let nodeList = [];
  let current = linkedList;
  while(current) {
    nodeList.push(current);
    current = current.next;
  }
  const lastIndex = nodeList.length - 1;
  for (let i = 0; i < lastIndex; i++) {
    nodeList[i + 1].next = nodeList[i];
  }
  if (lastIndex >= 0) {
    nodeList[0].next = null;
    return nodeList[lastIndex];
  }
}

Hey guys,

I proudly present my code solution to the reverse linked list challenge.


function reverseLinkedList(linkedList){

let currentNode = linkedList;
let previousNode = null;
let newNode = null;

while(currentNode !== null) {
  newNode = new Node(currentNode.data)
  newNode.next = previousNode;
  previousNode = newNode;
  currentNode = currentNode.next;
}

return newNode;
}

I appear to have been ever lazier :sweat_smile:


function reverseLinkedList(linkedList){
  const listArray = [];
  let current = linkedList;

  while (current) {
    listArray.push(current.data);
    current = current.next;
  }
  listArray.reverse()

  return makeLinkedList(listArray);

}

Finally solved it, using the first function.
Had to look it up, not being able to fix it at first.
First i wrote the second function, that worked but didn’t pass.
Tried to keep the runtime shorter by implementing edge cases.
Do you think the edge cases are helping? Or neglectable?

Such a simple elegant solution, for what seems like a difficult problem;
PS: the code below will throw an error, based on the missing imports

const makeLinkedList = require('./makeLinkedList.js'); // O(n) time & O(1) space function reverseLinkedList(head){ let prev = null; let next = null; // we are going to reverse the arrows /pointers //the first node will become the last, and will therefore point to null // we will do this by storing the head's pointer first in a a varibale.pointing it to the previous node'. //first we have to store the head's next node in a a variable, to save it for the next iteration while (head !== null){ //first we have to store the head's next node in a a variable, to save it for the next iteration next = head.next; //then we reset the head nodes pointer to the previous node. in the first iteration, this will be null head.next = prev; //now, we set the prev variable to the head, for the next iteration. This is so in the next iteration, the head.next can be set to the previous node. prev = head; //now we continue the iteration cycle, by changing the head variable to the previously stored next node: the next node in the original list for processing. head = next; } //when head > the last node of the list, the loop breaks. we now return prev, since that it is the last processed node of the iteration cycle, therfore making it the new head return prev; } function reverseLinkedListX(linkedList){ // Write your code here let prev = null; let head = linkedList; let next = null; //check if the list has two or more items if (head.next === null){ console.log('Only one item in the list, no need to reverse!'); return head; } //continue if it has more than one item next = head.next; //if it only has two items if (next.next === null){ next.next = head; head.next = prev; return next; } else { // has more then two items while (head !== null){ next = head.next; head.next = prev; prev = head head = next; } return prev; } } //Calling your function on an example console.log("Original") let exampleLinkedList = makeLinkedList([3,2,1]); exampleLinkedList.print() console.log("Reversed") let reversed = reverseLinkedList(exampleLinkedList); reversed.print() // Leave this here so we can test your code module.exports = reverseLinkedList;