0

I understand that there are a few reverse linked list questions using javascript, but I was hoping for more clarity on a solution that someone else had provided.

Is the use of a class LinkedList necessary to declare a linkedlist or would just doing it like this example given work?

I'm just not clear as to how this function is just declaring a variable called reverseLinkedList and solving it as opposed to actually using the class.

Not sure if that made any sense, but otherwise, would this solution be clear enough to solve the reversing a linked list problem?

Also, would this be considered a O(n) for time complexity? As it has a while loop that is set for a finite amount of time to run. And i'm not sure about space complexity.

// reverse a linked list
const reverseLinkedList = (linkedlist) => {
  let node = linkedlist;
  let previous = null;

  while(node) {
// save next or you lose it!!!
  let save = node.next;
// reverse pointer
  node.next = previous;
// increment previous to current node
  previous = node;
// increment node to next node or null at end of list
  node = save;
}
  return previous;   // Change the list head !!!
}
linkedlist = reverseLinkedList(linkedlist);
mph85
  • 994
  • 3
  • 11
  • 33

2 Answers2

1

There are a couple things going on here

First this declaration

let reverseLinkedList = function(linkedlist) {

is similar to this declaration

function reverseLinkedList(linkedList) {

except that you can't call reverseLinkList until it's been declared in the first example. You can read more about hoisting here

var functionName = function() {} vs function functionName() {}

Second using a class or function doesn't really matter all that much. You will see that classes in Javascript are really just syntactic sugar (but there are some gotcha's you can read about here)

are es6 classes just syntactic sugar for the prototypal pattern in javascript?

Lastly the time complexity of this function is O(n) because it just loops through the list once.

Hope that helps!

Asleepace
  • 2,369
  • 14
  • 27
  • Appreciate the clarification, i revised the function in ES6 format, would there be an issue there with that syntax? And when you mention the complexity of the function as O(n), you're referring to time complexity right? what about space complexity? – mph85 Mar 13 '19 at 23:47
  • @mph85 as long as you don't call the function before it's declared it's fine. Right that is the time complexity, the additional space complexity is O(1) since you only use 3 extra temp variables. O(3) is O(1). – Asleepace Mar 13 '19 at 23:51
  • So just to clarify, `let node` `let previous`, and `let save` are the 3 temporary variables and therefore only take up, after everything has been factored down, to O(1)? – mph85 Mar 13 '19 at 23:57
1

Either way is fine. Making a class LinkedList would allow you to add methods to it, but it's overkill for solving a single reverse linked list problem.

A linked list is just a collection of nodes, where each node references the next one with a next object property.

If you wanted to add a bunch of convenience methods to the LinkedList, then yeah, a class would be useful. But for a single problem like list reversal, the solution you posted assumes that the function takes as input the first node in the list and that each node (just a JavaScript object) has a next property.

For example, say I was solving multiple linked list problems and I also needed to create a linked list out of an array of values. Say I also wanted to easily test if a function (such as reverseLinkedList) returns the correct list. I could use a class like this

Then, to test the above solution would be as easy as:

First, rewrite the reverse solution so that it takes a linkedList rather than just the head & so you can modify the head property of the list after it's been reversed:

const reverseLinkedList = (linkedlist) => {
  let node = linkedlist.head;
  let previous = null;

  while(node) {
// save next or you lose it!!!
  let save = node.next;
// reverse pointer
  node.next = previous;
// increment previous to current node
  previous = node;
// increment node to next node or null at end of list
  node = save;
}
  linkedlist.head = previous
  return previous;   // Change the list head !!!
}

Then you can run this test:

var inputList = new LinkedList([1,2,3,4])
var expectedReversedList = new LinkedList([4,3,2,1])
var actualReversedList = reverseLinkedList(inputList)
console.log(inputList.isEqualTo(actualReversedList))

Also, in terms of space and time complexity, since the above solution mutates the objects, it's O(1) space and since it iterates through the nodes just once, it's O(N) time complexity

Community
  • 1
  • 1