CursosEmpresasBlogLiveConfPrecios

Solución de Reorder List

Clase 18 de 41 • Curso de Algoritmos Avanzados: Estructuras de Datos Lineales

Clase anteriorSiguiente clase
    Carlos Hernandez

    Carlos Hernandez

    student•
    hace 10 meses
    class Nodo { constructor(valor) { this.valor = valor; this.siguiente = null; } } function reorderList(head) { // Tu código aquí 👇 let slow = head, fast = head; let leftHalf = [slow]; let rightHalf = []; while (fast.siguiente && fast.siguiente.siguiente) { fast = fast.siguiente.siguiente; slow = slow.siguiente; leftHalf.push(slow); } while (slow.siguiente) { rightHalf.push(slow.siguiente); slow = slow.siguiente; } const maxLength = Math.max(leftHalf.length, rightHalf.length); for (let index = 0; index < maxLength; index++) { const currentLeft = leftHalf[index]; const currentRight = rightHalf[rightHalf.length - 1 - index]; currentLeft.siguiente = currentRight ? currentRight : null; if (currentRight) { currentRight.siguiente = leftHalf[index + 1] ? leftHalf[index + 1] : null; } } return leftHalf[0]; } function clon(obj) { return JSON.parse(JSON.stringify(obj)); } // Input const nodo1 = new Nodo("a"); const nodo2 = new Nodo("b"); const nodo3 = new Nodo("c"); const nodo4 = new Nodo("d"); nodo1.siguiente = nodo2; nodo2.siguiente = nodo3; nodo3.siguiente = nodo4; console.log(reorderList(nodo1).siguiente); ```class Nodo {    constructor(valor) {      this.valor = valor;      this.siguiente = null;    }  }  function reorderList(head) {    // Tu código aquí 👇    let slow = head, fast = head;    let leftHalf = \[slow];    let rightHalf = \[];     while (fast.siguiente && fast.siguiente.siguiente) {        fast = fast.siguiente.siguiente;        slow = slow.siguiente;        leftHalf.push(slow);    }     while (slow.siguiente) {        rightHalf.push(slow.siguiente);        slow = slow.siguiente;    }     const maxLength = Math.max(leftHalf.length, rightHalf.length);     for (let index = 0; index < maxLength; index++) {        const currentLeft = leftHalf\[index];        const currentRight = rightHalf\[rightHalf.length - 1 - index];        currentLeft.siguiente = currentRight ? currentRight : null;         if (currentRight) {            currentRight.siguiente = leftHalf\[index + 1] ? leftHalf\[index + 1] : null;        }    }     return leftHalf\[0];} function clon(obj) {    return JSON.parse(JSON.stringify(obj));} // Inputconst nodo1 = new Nodo("a");const nodo2 = new Nodo("b");const nodo3 = new Nodo("c");const nodo4 = new Nodo("d");nodo1.siguiente = nodo2;nodo2.siguiente = nodo3;nodo3.siguiente = nodo4; console.log(reorderList(nodo1).siguiente);
    Francisco Roberto Gomez

    Francisco Roberto Gomez

    student•
    hace 2 años

    Se me ocurre también una implementacion similar a Odd Even List teniendo 2 referencias fijas. Una para mi inicio y otra para la mitad. De esa forma ir intercalando los elementos.

    Nicolas Alpargatero

    Nicolas Alpargatero

    student•
    hace 2 años

    ++Resultados en Python:++

    results_reorder.png
    .
    results_reorder_test.png

    Implementación:

    lo que más me genero dificultad es que me entraba en infinite loop, y era por que me faltaba el middle.next = None y el current.next.next = next_node para que conecte con la primera lista.

    reorder_list_py.png

Escuelas

  • Desarrollo Web
  • English Academy
  • Marketing Digital
  • Inteligencia Artificial y Data Science
  • Ciberseguridad
  • Liderazgo y Habilidades Blandas
  • Diseño de Producto y UX
  • Contenido Audiovisual
  • Desarrollo Móvil
  • Diseño Gráfico y Arte Digital
  • Programación
  • Negocios
  • Blockchain y Web3
  • Recursos Humanos
  • Finanzas e Inversiones
  • Startups
  • Cloud Computing y DevOps

Platzi y comunidad

  • Platzi Business
  • Live Classes
  • Lanzamientos
  • Executive Program
  • Trabaja con nosotros
  • Podcast

Recursos

  • Manual de Marca

Soporte

  • Preguntas Frecuentes
  • Contáctanos

Legal

  • Términos y Condiciones
  • Privacidad
Reconocimientos
Reconocimientos
Logo reconocimientoTop 40 Mejores EdTech del mundo · 2024
Logo reconocimientoPrimera Startup Latina admitida en YC · 2014
Logo reconocimientoPrimera Startup EdTech · 2018
Logo reconocimientoCEO Ganador Medalla por la Educación T4 & HP · 2024
Logo reconocimientoCEO Mejor Emprendedor del año · 2024
De LATAM conpara el mundo
YoutubeInstagramLinkedInTikTokFacebookX (Twitter)Threads