Introducción

1

¿Ya tomaste el Curso Avanzado de Algoritmos: Patrones de Arrays y Strings?

Lista Enlazada

2

Estructura de datos: Lista Enlazada

3

Programando listas enlazadas con Java

4

Cómo Invertir una Lista Enlazada

5

Odd Even Linked List: análisis del problema

6

Solución de Odd Even Linked List

7

Playground: Odd Even Liked List

8

Programando Odd Even Linked List con C++

9

Linked List Cycle: análisis del problema

10

Solución de Linked List Cycle

11

Playground: Linked List Cycle

12

Programando Linked List Cycle con Python

13

Palindrome Linked List: análisis del problema

14

Solución de Palindrome Linked List

15

Playground: Palindrome Linked List

16

Programando Palindrome Linked List con Java

17

Reorder List: análisis del problema

18

Solución de Reorder List

19

Programando Reorder List con JavaScript

20

Playground: Reorder List Without Repeated Values

21

Reto: LRU Caché

22

Ejercicios Prácticos con Listas Enlazadas y Historial de Navegador

23

Operaciones con Listas Enlazadas: Suma, Intercambio y Navegador

Pilas y colas

24

Estructura de datos: Pilas y Colas

25

Paréntesis Válido: análisis del problema

26

Solución de Paréntesis Válido

27

Playground: Paréntesis Válido

28

Programando Paréntesis Válido con C++

29

Comparación de Cadenas con Backspaces

Colas de prioridad

30

Estructura de datos: Colas de Prioridad

31

K Closest Points to Origin: análisis del problema

32

Solución de K Closest Points to Origin

33

Playground: K Closest Points to Origin

34

Programando K Closest Points to Origin con Python

35

Reorganize String: análisis del problema

36

Solución de Reorganize String

37

Playground: Reorganize String

38

Programando Reorganize String con Python

39

Colas de Prioridad: Ejercicios Prácticos y Soluciones

40

Colas de Prioridad y Gestión de Eventos

Próximos pasos

41

Toma el Curso Avanzado de Algoritmos: Grafos y Árboles

No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Solución de Reorder List

18/41
Recursos

Aportes 3

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

```js 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);

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.

Resultados en Python:

.

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.