Tell us what’s happening:
Good morning!! I don’t know where the error is…Can someone help me solve it?
Your code so far
function LinkedList() {
var length = 0;
var head = null;
var Node = function(element){ // {1}
this.element = element;
this.next = null;
};
this.size = function(){
return length;
};
this.head = function(){
return head;
};
this.add = function(element){
var node = new Node(element);
if(head === null){
head = node;
} else {
var currentNode = head;
while(currentNode.next){
currentNode = currentNode.next;
}
currentNode.next = node;
}
length++;
};
// Only change code below this line
this.removeAt = function(index){
if (index >= length || index < 0){return null}
let currentNode = head;
let previousNode;
var c = 0;
while (c !== index) {
previousNode = currentNode;
currentNode = currentNode.next;
c++;
}
previousNode.next = currentNode.next;
length--;
return currentNode.element;//return removed element
}
// Only change code above this line
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.
Challenge: Remove Elements from a Linked List by Index
Link to the challenge: