Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
Trabajando en mi primera lista vinculada y no puedo hacer que la función de devolución funcione correctamente

Aquí está el código que he recopilado y trabajado hasta ahora. Parece que no puedo hacer que mi función de eliminación funcione correctamente. Sigue diciendo "No se pueden leer las propiedades de undefined (leyendo 'valor') cuando intento ejecutarlo en las dos últimas líneas. Cualquier consejo o ayuda sería apreciado, ¡gracias!

 function LinkedList() { this.head = null; this.tail = null; this.length = 0; } function Node(value) { this.value = value; this.next = null; } // adds a node to the specified index // if index is specified, accepts parameter (value, index) // if no index is specified then add element to the end of list LinkedList.prototype.add = function(...value) { if (!this.head) { this.head = this.tail = new Node(...value); this.length++; return; } while (value.length) { let curr = value.pop(); this.tail.next = new Node(curr); this.tail = this.tail.next; this.length++; } } // retrieves the node at the specified index LinkedList.prototype.get = function(index) { if (index >= this.length) { return -1; } if (index === 0) { return this.head; } let previousNode = null; let currentNode = this.head; for (let i = 0; i < index; i++) { if (!currentNode.next) { break; } previousNode = currentNode; currentNode = currentNode.next; } previousNode.next = currentNode.next; this.length--; return currentNode; } // retrieves and removes the node at the specified index // if no index is specified, removes the last node (tail) LinkedList.prototype.remove = function(index) { if (this.length === 0) { return undefined; } if (this.head.value === index) { this.removeFromHead(); return this; } let previousNode = this.head; let thisNode = previousNode.next; while (thisNode) { if (thisNode.value === index) { break; } previousNode = thisNode; thisNode = thisNode.next; } if (thisNode === null) { return undefined; } previousNode.next = thisNode.next; this.length--; return this; } LinkedList.prototype.removeFromHead = function() { if (this.length === 0) { return undefined; } const value = this.head.value; this.head = this.head.next; this.length--; return value; } let linkedList = new LinkedList() linkedList.add(0); linkedList.add(1, 0); linkedList console.log(linkedList.remove().value) //should return 1 console.log(linkedList.remove().value) //should return 2

No soy del todo lindo

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda