I'm a beginner in coding and recently studying linked list on JavaScript.
I am confused that while removing node, should I also change the removing node’s pointer to null?
RemoveAt(index) method is a custom method for removing the node at a specific index.
Example as below:
// remove node
removeAt(index){
// check if index is qualified
if(index < 0 || index > this.length) return null;
let current = this.head,
prev = null,
idx = 0;
// if first node
if(index===0){
// point the head to next node
this.head = current.next;
}else{
// else find the index
while(idx++ < index){
prev = current; // set prev node as current
current = current.next; // and move current to the next node
}
// index finded, link prev.next & current.next
prev.next = current.next;
}
this.length--;
}
Most of the linked list examples I saw in JS omitted this. I am wondering will it waste more memory?