CursosEmpresasBlogLiveConfPrecios

Playground: Odd Even Liked List

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

Clase anteriorSiguiente clase
    Pro Code

    Pro Code

    student•
    hace 2 años

    ++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.

      Pro Code

      Pro Code

      student•
      hace 2 años

      Mi código:

      export function oddEvenList(cabeza) { if (cabeza == null || (cabeza.siguiente == null)) { // Si la lista es nula o tiene solo 1 Nodo return cabeza; } let anterior = cabeza; let actual = cabeza.siguiente; let cola = actual; while (cola.siguiente != null) { //Para obtener el Nodo cola cola = cola.siguiente; } let colaOriginal = cola; let aux = null; while (actual.siguiente != null) { aux = actual.siguiente; if (anterior == (colaOriginal)) { // Romper el ciclo si la lista enlazada es impar break; } anterior.siguiente = actual.siguiente; cola.siguiente = actual; actual.siguiente = null; cola = actual; if (cola == (colaOriginal)) { // romper el ciclo si la lista enlazada es par break; } anterior = aux; actual = aux.siguiente; } return cabeza; }```
    John Eduard Meneses González

    John Eduard Meneses González

    student•
    hace 8 meses

    My solution!!

    Cesar Augusto Mora Castilo

    Cesar Augusto Mora Castilo

    student•
    hace un año

    Clases y métodos lista enlazada version Python

    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()
    Cesar Augusto Mora Castilo

    Cesar Augusto Mora Castilo

    student•
    hace un año

    Classes y métodos lista enlazada en 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();
    Santiago Amaya

    Santiago Amaya

    student•
    hace 2 años
    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
    Rafael Estiven Uribe Álvarez

    Rafael Estiven Uribe Álvarez

    student•
    hace 2 años

    Mi respuesta:

    Juan Pablo Reina Gutierrez

    Juan Pablo Reina Gutierrez

    student•
    hace 2 años

    Esta es mi solución:

    solucion_odd_even_linked_list_2_1.jpg
    solucion_odd_even_linked_list_2_2.jpg
    Nicolas Alpargatero

    Nicolas Alpargatero

    student•
    hace 2 años

    Pasaron las pruebas, fue de gran ayuda el Playground

    test_playground.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