Good morning! Here's our prompt for today.
You're sent a linked list of numbers, but it's been received in the opposite order to what you need. This has happened multiple times now, so you decide to write an algorithm to reverse the lists as they come in. The list you've received is as follows:
SNIPPET
17 -> 2 -> 21 -> 6 -> 42 -> 10Write an algorithm for a method reverseList that takes in a head node as a parameter, and reverses the linked list. It should be capable of reversing a list of any length.

You may use the example linked list for testing purposes. Your method will be called as such:
1class LinkedListNode {
2 constructor(val, next = null) {
3 this.val = val;
4 this.next = next;
5 }
6}
7
8l1 = new LinkedListNode(1);
9l1.next = new LinkedListNode(2);
10reverseList(l1);Constraints
- Length of the given LinkedList <=
100000 - The nodes will always contain integer values between
-1000000000and1000000000 - Expected time complexity :
O(n) - Expected space complexity :
O(1)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx57
'PASSED: Reversing linked list `3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10` should return `10 -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3`'var assert = require('assert');​function reverseList(head) { // Fill in this method return head;}​function Node(val) { this.val = val; this.next = null;}​function LinkedListNode(val) { this.val = val; this.next = null;}​var list1 = new LinkedListNode(3);var nodes1 = [4, 5, 6, 7, 8, 9, 10];createNodes(list1, nodes1);​var list2 = new LinkedListNode(1);var nodes2 = [2, 3, 4, 5, 6, 7, 8];createNodes(list2, nodes2);​function createNodes(head, nodes) { for (let i = 0; i < nodes.length; i++) { var newNode = new LinkedListNode(nodes[i]); head.next = newNode;OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
We'll now take you through what you need to know.
How do I use this guide?

