I'm stuck on an exercise where I have a method remove()
that should fetch the last entered node from a linked list and return its value. The first code of the current variable returns null if the list is empty. Now what I have to solve is that I should get the last entered node and return its value (taking into account if I have a list of only one NODE and an empty list).
// TEST
it('remove deberia sacar el ultimo nodo ingresado y devolver su valor', function() {
linkedList.add('first');
linkedList.add('second');
expect(linkedList.remove()).toBe('second');
expect(linkedList.remove()).toBe('first');
});
// CODIGO
function LinkedList() {
this.head = null;
}
function Node(value) {
this.value = value;
this.next = null;
}
// METODO REMOVE QUE ESTUVE PROBRANDO
LinkedList.prototype.remove = function() {
let current = this.head
if(current === null)
return null
if(current.next === null){
return null
}
let aux = this.head
while(aux.next.next != null){
aux = aux.next;
}
aux.next = null;
return aux
}