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 recomendados de Lista Enlazada

23

Ejercicios resueltos de Lista Enlazada

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

Ejercicios recomendados de Pilas

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

Ejercicios recomendados de Colas de prioridad

40

Ejercicios resueltos de Colas de prioridad

Pr贸ximos pasos

41

Toma el Curso Avanzado de Algoritmos: Grafos y 脕rboles

You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
7 Hrs
36 Min
55 Seg

Playground: Odd Even Liked List

7/41

Contributions 8

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

Mi soluci贸n se basa en (Si la lista fuera: 3 -> 4 -> 5 -> 6 y mis 铆ndices 1,2,3,4):

Recorrer la lista tomando como Nodo actual los nodos de 铆ndice par (empezando a contar mis 铆ndices desde 1, Nodo cabeza es el 铆ndice 1), entonces Nodo actual ingresar铆a con el Nodo de 铆ndice 2, es decir Nodo con valor 4, tomo el anterior y lo apunto hacia el siguiente de actual:
3 -> 4 -> 5 -> 6, entonces 3 -> 5 -> 6

Luego a Nodo actual lo muevo hacia la cola de la lista, quedando de la siguiente manera:
3 -> 5 -> 6 -> 4

Vuelve a ingresar al while, ahora como Nodo actual con Nodo de 铆ndice 4 (es decir el Nodo con valor 6, el que ten铆a 铆ndice 4 originalmente) y vuelvo a realizar el mismo proceso:
3 -> 5 -> 4
Muevo el actual a la cola de la lista:
3 -> 5 -> 4 -> 6

Para que culmine la iteraci贸n he guardado el Nodo cola original, para compararlo y romper el ciclo.

My solution!! ![](https://static.platzi.com/media/user_upload/image-a5917d18-010f-45fe-8e21-7ac46c650b4d.jpg)
Clases y m茅todos lista enlazada version Python ```js class lista_enlazada: def __init__(self): self.head = None self.tail = None def insertar_nodo_al_inicio(self, value): new_node = Node(value) new_node.next = self.head self.head = new_node def insertar_nodo_al_final(self, value): new_node = Node(value) # create new node if self.head is None: # empty list self.head = new_node return last = self.head while last.next: # find the las node last = last.next last.next = new_node # new node is the next of former last def imprimir_list(self): temp = self.head # Start from the head of the list while temp: print(temp.value, end=' ') # print the data in the current node temp = temp.next # move to the next node print() def borrar_nodo_al_inicio(self): if self.head == None: return "The list is empty" # If empty list # Remove first making the next node the new head self.head = self.head.next def borrar_nodo_al_final(self): if self.head == None: return "The list is empty" # If empty list if self.head.next is None: self.head = None # If there's only one node, remove the head by making it None return temp = self.head while temp.next.next: temp = temp.next # Remove the last node by setting the next pointer of the second-last node to None temp.next = None def search(self, search_value): current = self.head position = 0 while current: if current.value == search_value: return f"Valor '{search_value} encontrado en la posici貌h {position}." current = current.next position += 1 return f"Valor '{search_value} no encontrado en la lista." def borrar_nodo_por_valor(self, value): if self.head == None: return "The list is empty" # If empty list # Remove first making the next node the new head current = self.head while current: if current.next.value == value: # Replace current next skiping next nodo wich haves the value current.next = current.next.next return current = current.next return f"Valor '{value} no encontrado en la lista." def invertir_lista(self): previous = None current = self.head while current is not None: current.next, previous, current = previous, current, current.next self.head = previous return previous.value # return self.head.value def odd_even_list(self): if self.head is None or self.head.next is None: return self.head odd = self.head head = odd even = self.head.next even_head = even while even is not None and even.next is not None: odd.next = even.next odd = even.next even.next = odd.next even = even.next odd.next = even_head return head.value class Node: def __init__(self, value): self.value = value self.next = None # Test class linked list llist = lista_enlazada() llist.insertar_nodo_al_inicio("rapido") llist.insertar_nodo_al_inicio("marr贸n") llist.insertar_nodo_al_inicio("zorro") llist.insertar_nodo_al_inicio("el") llist.imprimir_list() llist.insertar_nodo_al_final("salta") llist.imprimir_list() llist.borrar_nodo_al_inicio() llist.borrar_nodo_al_final() llist.imprimir_list() llist.insertar_nodo_al_inicio("el") llist.insertar_nodo_al_final("salta") llist.imprimir_list() print(llist.search('zorro')) # Expected to find print(llist.search('rapido')) # Expected to find print(llist.search('lazy')) # Expected not to find llist.borrar_nodo_por_valor("salta") llist.imprimir_list() print("Head: ", llist.invertir_lista()) llist.imprimir_list() oddeven = lista_enlazada() oddeven.insertar_nodo_al_inicio(4) oddeven.insertar_nodo_al_inicio(3) oddeven.insertar_nodo_al_inicio(2) oddeven.insertar_nodo_al_inicio(1) # print("Head: ", oddeven.invertir_lista()) print("head:",oddeven.odd_even_list()) oddeven.imprimir_list() ```class lista\_enlazada: def \_\_init\_\_(self): self.head = None self.tail = None def insertar\_nodo\_al\_inicio(self, value): new\_node = Node(value) new\_node.next = self.head self.head = new\_node def insertar\_nodo\_al\_final(self, value): new\_node = Node(value) *# create new node* ** if self.head is None: *# empty list* ** self.head = new\_node return last = self.head while last.next: *# find the las node* ** last = last.next last.next = new\_node *# new node is the next of former last* ** def imprimir\_list(self): temp = self.head *# Start from the head of the list* ** while temp: print(temp.value, end=' ') *# print the data in the current node* ** temp = temp.next *# move to the next node* ** print() def borrar\_nodo\_al\_inicio(self): if self.head == None: return "The list is empty" *# If empty list* *# Remove first making the next node the new head* ** self.head = self.head.next def borrar\_nodo\_al\_final(self): if self.head == None: return "The list is empty" *# If empty list* ** if self.head.next is None: self.head = None *# If there's only one node, remove the head by making it None* ** return temp = self.head while temp.next.next: temp = temp.next *# Remove the last node by setting the next pointer of the second-last node to None* ** temp.next = None def search(self, search\_value): current = self.head position = 0 while current: if current.value == search\_value: return f"Valor '{search\_value} encontrado en la posici貌h {position}." current = current.next position += 1 return f"Valor '{search\_value} no encontrado en la lista." def borrar\_nodo\_por\_valor(self, value): if self.head == None: return "The list is empty" *# If empty list* *# Remove first making the next node the new head* ** current = self.head while current: if current.next.value == value: *# Replace current next skiping next nodo wich haves the value* ** current.next = current.next.next return current = current.next return f"Valor '{value} no encontrado en la lista." def invertir\_lista(self): previous = None current = self.head while current is not None: current.next, previous, current = previous, current, current.next self.head = previous return previous.value *# return self.head.value* ** def odd\_even\_list(self): if self.head is None or self.head.next is None: return self.head odd = self.head head = odd even = self.head.next even\_head = even while even is not None and even.next is not None: odd.next = even.next odd = even.next even.next = odd.next even = even.next odd.next = even\_head return head.value class Node: def \_\_init\_\_(self, value): self.value = value self.next = None *# Test class linked list* llist = lista\_enlazada() llist.insertar\_nodo\_al\_inicio("rapido") llist.insertar\_nodo\_al\_inicio("marr贸n") llist.insertar\_nodo\_al\_inicio("zorro") llist.insertar\_nodo\_al\_inicio("el") llist.imprimir\_list() llist.insertar\_nodo\_al\_final("salta") llist.imprimir\_list() llist.borrar\_nodo\_al\_inicio() llist.borrar\_nodo\_al\_final() llist.imprimir\_list() llist.insertar\_nodo\_al\_inicio("el") llist.insertar\_nodo\_al\_final("salta") llist.imprimir\_list() print(llist.search('zorro')) *# Expected to find* print(llist.search('rapido')) *# Expected to find* print(llist.search('lazy')) *# Expected not to find* llist.borrar\_nodo\_por\_valor("salta") llist.imprimir\_list() print("Head: ", llist.invertir\_lista()) llist.imprimir\_list() oddeven = lista\_enlazada() oddeven.insertar\_nodo\_al\_inicio(4) oddeven.insertar\_nodo\_al\_inicio(3) oddeven.insertar\_nodo\_al\_inicio(2) oddeven.insertar\_nodo\_al\_inicio(1) *# print("Head: ", oddeven.invertir\_lista())* print("head:",oddeven.odd\_even\_list()) oddeven.imprimir\_list()
Classes y m茅todos lista enlazada en JS ```js class linked_list { constructor() { this.head = null; this.tail = null; } insert_node_at_start(value) { let new_node = new Node(value); new_node.next = this.head; this.head = new_node; } insert_node_at_end(value) { let new_node = new Node(value); if (this.head == null) { this.head = new_node; return; } let last = this.head; while (last.next) { last = last.next; } last.next = new_node; } print_list() { let temp = this.head; while (temp) { console.log(temp.value); temp = temp.next; } } delete_start_node() { if (this.head == null) { return "The list is empty"; } this.head = this.head.next; } delete_last_node() { if (this.head == null) { return "The list is empty"; } if (this.head.next == null) { this.head = null; return; } let temp = this.head; while (temp.next.next) { temp = temp.next; } temp.next = null; } search(search_value) { let current = this.head; let position = 0; while (current) { if (current.value == search_value) { console.log("Valor encontrado: ", current.value); return current.value; } current = current.next; position++; } return console.log("Valor no encontrado\n"); } delete_value(value) { if (this.head == null) { return console.log("Empty list"); } let current = this.head; while (current) { if (current.next.value == value) { current.next = current.next.next; return; } current = current.next; } return console.log("Valor no encontrado en la lista"); } reverse_list() { this.print_list(); let previous = null; let current = this.head; while (current !== null) { let temp = current.next; current.next = previous; previous = current; current = temp; //current.next; this.head = previous; } return console.log("Head: ", previous.value); } odd_even() { if (this.head == null || this.head.next == null) { return this.head; } let odd = this.head; let head = odd; let even = this.head.next; let even_head = even; while (even != null && even.next != null) { odd.next = even.netx; odd = even.next; event.next = odd.next; even = even.next; } odd.next = even_head; return head.value; } } class Node { constructor(value) { this.value = value; this.next = null; } } const llist = new linked_list(); llist.insert_node_at_start("4"); llist.insert_node_at_start("3"); llist.insert_node_at_start("2"); llist.insert_node_at_start("1"); llist.reverse_list(); llist.print_list(); llist.reverse_list(); llist.print_list(); ```class linked\_list { constructor() { *this*.head = null; *this*.tail = null; } insert\_node\_at\_start(value) { let new\_node = new Node(value); new\_node.next = *this*.head; *this*.head = new\_node; } insert\_node\_at\_end(value) { let new\_node = new Node(value); if (*this*.head == null) { *this*.head = new\_node; return; } let last = *this*.head; while (last.next) { last = last.next; } last.next = new\_node; } print\_list() { let temp = *this*.head; while (temp) { console.log(temp.value); temp = temp.next; } } delete\_start\_node() { if (*this*.head == null) { return "The list is empty"; } *this*.head = *this*.head.next; } delete\_last\_node() { if (*this*.head == null) { return "The list is empty"; } if (*this*.head.next == null) { *this*.head = null; return; } let temp = *this*.head; while (temp.next.next) { temp = temp.next; } temp.next = null; } search(search\_value) { let current = *this*.head; let position = 0; while (current) { if (current.value == search\_value) { console.log("Valor encontrado: ", current.value); return current.value; } current = current.next; position++; } return console.log("Valor no encontrado\n"); } delete\_value(value) { if (*this*.head == null) { return console.log("Empty list"); } let current = *this*.head; while (current) { if (current.next.value == value) { current.next = current.next.next; return; } current = current.next; } return console.log("Valor no encontrado en la lista"); } reverse\_list() { *this*.print\_list(); let previous = null; let current = *this*.head; while (current !== null) { let temp = current.next; current.next = previous; previous = current; current = temp; *//current.next;* *this*.head = previous; } return console.log("Head: ", previous.value); } odd\_even() { if (*this*.head == null || *this*.head.next == null) { return *this*.head; } let odd = *this*.head; let head = odd; let even = *this*.head.next; let even\_head = even; while (even != null && even.next != null) { odd.next = even.netx; odd = even.next; event.next = odd.next; even = even.next; } odd.next = even\_head; return head.value; }} class Node { constructor(value) { *this*.value = value; *this*.next = null; }} const llist = new linked\_list(); llist.insert\_node\_at\_start("4");llist.insert\_node\_at\_start("3");llist.insert\_node\_at\_start("2");llist.insert\_node\_at\_start("1"); llist.reverse\_list();llist.print\_list();llist.reverse\_list();llist.print\_list();
```python def oddeven(self): counter = 0 current = self.head even_head = None even_tail = None odd_head = None odd_tail = None while current is not None: counter += 1 if counter % 2 == 0: # Check if the index is even if even_head is None: even_head = current even_tail = current else: even_tail.next = current even_tail = even_tail.next else: if odd_head is None: odd_head = current odd_tail = current else: odd_tail.next = current odd_tail = odd_tail.next current = current.next # Connect the even and odd lists if even_head is not None: even_tail.next = None if odd_head is not None: odd_tail.next = even_head return odd_head ```
Mi respuesta: ![](https://static.platzi.com/media/user_upload/image-07083190-a40b-4fc7-87b6-b3e60e720f9c.jpg)

Esta es mi soluci贸n:

Pasaron las pruebas, fue de gran ayuda el Playground

undefined