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

239
Vistas
Insertar elemento en un índice específico en una lista enlazada (Javascript)

No puedo entender por qué mi código no funciona y lo he estado mirando por siempre. Cuando trato de llamar a la función, aparece el error "No se pueden leer las propiedades de nulo (leyendo 'siguiente')" y no esperaba que esto sucediera ya que estoy reasignando tanto curr como prev para iterar a través de la lista.

 // insert an item at the position specified LinkedList.prototype.insert = function(data, position) { let newNode = new Node(data); //instantiate a new node if (position < 0 || position > this.size) { // if the position is less than zero or greater than the size of the linked list itself return 'Please enter a valid position.' // return error statement } else { if (this.head === null) { // if we do not have any nodes in the list) this.head = newNode; // point newNode's pointer to the head this.tail = newNode; // point the head pointer to the newNode; } else { let prev = null; // set up a variable to hold the prev value let curr = this.head; // set a current variable to this.head (the start) let currIndex = 0; while (currIndex < position) { // while the current index we're at is less than the position, we need to keep iterating prev = curr; // set prev variable equal to curr curr = curr.next; // skip to next node until we find the right position } newNode.next = curr; // after we find the position, set the new node's next to equal the current prev.next = newNode; // also set the previous node's next to equal the new node } this.size++; } } console.log(myList) myList.insert(4,1) console.log(myList)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Cerrando esto cuando descubrí lo que estaba mal. Me estaba olvidando de incrementar currIndex dentro del bucle while, jaja.

about 4 years ago · Juan Pablo Isaza Denunciar
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