Hola estoy intentando armar esta un método para borrar un dato en una linked list, ya implemente los métodos de agregar por índice y agregar al principio, pero con el de borrar vengo peleando hace un rato largo, creo que la base empezaria asi, pero no se como seguir, si alguien me podría ayudar se lo agradecería mucho.
class Node {
// Accept arguments (the second one could be optional)
constructor(data, next=null) {
this.data = data;
this.next = next;
}
lastNode() { // new method that uses recursion
return this.next?.lastNode() || this;
}
}
class ListRecurse {
constructor() {
this.head = null;
this.size = 0;
}
add(data) {
let newNode = new Node(data); // No second argument. It has a default value
if (this.head === null) {
this.head = newNode;
} else {
// The lastNode implementation uses recursion:
this.head.lastNode().next = newNode;
}
this.size ++;
return this; // to allow chaining
}
DeletexData(data) {
let current = this.head;
let prev = null;
if (current != null) {
if (current.data === data && prev === null) {
this.head = current.next;
} else {
this.head.next = current.next
}
this.head.lastNode(data).next
}
return null;
}
}