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

245
Views
¿Cómo crear una lista vinculada en javascript?

Si quiero crear una lista enlazada con estos datos iniciales

 const linkedListData = { linkedList: { head: "1", nodes: [ { id: "1", next: "1-2", value: 1 }, { id: "1-2", next: "1-3", value: 1 }, { id: "1-3", next: "2", value: 1 }, { id: "2", next: "3", value: 3 }, { id: "3", next: "3-2", value: 4 }, { id: "3-2", next: "3-3", value: 4 }, { id: "3-3", next: "4", value: 4 }, { id: "4", next: "5", value: 5 }, { id: "5", next: "5-2", value: 6 }, { id: "5-2", next: null, value: 6 }, ], }, };

Pero el resultado de la lista vinculada debería verse como

 const linkedListDataV2 = { value: 1, next: { value: 1, next: { value: 1, next: { value: 3, }, next: { value: 4, next: { value: 4, next: { value: 4, next: { value: 5, next: { value: 6, next: { value: 6, next: null } } } } } } } } }

¿Hay algún algoritmo existente donde pueda crear una lista vinculada en javascript? La razón es que estoy tratando de depurar algunos algoritmos de listas vinculadas en el código vs, pero necesito encontrar una manera de crear fácilmente una lista vinculada en lugar de crearlas manualmente.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Puede tomar un objeto y asignar los nodos al nodo real y al siguiente. Como resultado tome la lista por el identificador principal.

Este enfoque también funciona para datos no ordenados.

 const linkedList = { head: "1", nodes: [{ id: "1", next: "1-2", value: 1 }, { id: "1-2", next: "1-3", value: 1 }, { id: "1-3", next: "2", value: 1 }, { id: "2", next: "3", value: 3 }, { id: "3", next: "3-2", value: 4 }, { id: "3-2", next: "3-3", value: 4 }, { id: "3-3", next: "4", value: 4 }, { id: "4", next: "5", value: 5 }, { id: "5", next: "5-2", value: 6 }, { id: "5-2", next: null, value: 6 }] }, list = linkedList.nodes.reduce((r, { id, next, value }) => { r[id] ??= {}; r[id].value = value; r[id].next = r[next] ??= {}; return r; }, {})[linkedList.head]; console.log(list);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Una versión en bucle (en lugar de usar reduce ). Similar en espíritu a la respuesta de Nina, y posiblemente más fácil de seguir.

 const linkedListData = { linkedList: { head: "1", nodes: [ { id: "1", next: "1-2", value: 1 }, { id: "1-2", next: "1-3", value: 1 }, { id: "1-3", next: "2", value: 1 }, { id: "2", next: "3", value: 3 }, { id: "3", next: "3-2", value: 4 }, { id: "3-2", next: "3-3", value: 4 }, { id: "3-3", next: "4", value: 4 }, { id: "4", next: "5", value: 5 }, { id: "5", next: "5-2", value: 6 }, { id: "5-2", next: null, value: 6 }, ], }, }; function nodesToList(nodes, headId) { // maps id -> {next: nextId, value: value} const byId = new Map(nodes.map( n => [n.id, {value: n.value, next: n.next}])); // maps id -> {next: node, value: value} for (let n of byId.values()) { n.next = n.next == null ? null : byId.get(n.next); } // and now, just return the head return byId.get(headId) } console.log(nodesToList( linkedListData.linkedList.nodes, linkedListData.linkedList.head));

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