Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
Adición del último nodo a una lista vinculada

Estoy aprendiendo cómo agregar un nodo a la lista vinculada. El siguiente es el código correcto:

 class NodeList { constructor(head = null){ this.head = head this.addLast = function (data) { let node = new Node(data) console.log(curr) if (this.head === null) { return this.head = node } else{ let curr = this.head while(curr.next){ curr = curr.next } curr.next = node } } } }

Inicialmente,

Escribí el código como tal:

 class NodeList { constructor(head = null){ this.head = head this.addLast = function (data) { let node = new Node(data) console.log(curr) if (this.head === null) { return this.head = node } else{ let curr = this.head while(curr.next){ curr = curr.next } curr.next = node } } } }

La principal diferencia está en la instrucción if. En vez de

 if(this.head === null) { return this.head = node}

escribí

 let curr = this.head if(curr === null) { return curr = node}

Sin embargo, el código no funciona.

Si hago algo como

let nodeList = new Nodelist nodeList.addLast(2)

Espero obtener la cabeza: Nodo {datos: 2, siguiente: nulo}. El primer código logró esa compra pero el segundo código no. Estoy confundido por qué esto es así. Ambos códigos tienen el mismo aspecto. ¿Alguien me puede iluminar?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!