++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.
Mi código:
exportfunctionoddEvenList(cabeza){if(cabeza ==null||(cabeza.siguiente==null)){// Si la lista es nula o tiene solo 1 Nodoreturn 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 imparbreak;} anterior.siguiente= actual.siguiente; cola.siguiente= actual; actual.siguiente=null; cola = actual;if(cola ==(colaOriginal)){// romper el ciclo si la lista enlazada es parbreak;} anterior = aux; actual = aux.siguiente;}return cabeza;}```
My solution!!
Clases y métodos lista enlazada version Python
classlista_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 newnodeif self.head is None: # empty list
self.head= new_node
return last = self.headwhile last.next: # find the las node
last = last.next last.next= new_node # newnode is the next of former last
def imprimir_list(self): temp = self.head # Startfrom the head of the list
whiletemp: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 newhead 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 Nonereturn temp = self.headwhile 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 =0whilecurrent:if current.value== search_value:return f"Valor '{search_value} encontrado en la posiciòh {position}." current = current.next position +=1return 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 newhead current = self.headwhilecurrent:if current.next.value== value: # Replace current next skiping next nodo wich haves the value
current.next= current.next.nextreturn current = current.nextreturn f"Valor '{value} no encontrado en la lista." def invertir_lista(self): previous =None current = self.headwhile 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.valueclassNode: def __init__(self, value): self.value= value
self.next=None# Testclasslinked 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()```classlista\_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 newnode***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 *# newnode is the next of former last*** def imprimir\_list(self):  temp = self.head*# Startfrom the head of the list***whiletemp: 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 newhead*** 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 whilecurrent: 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 newhead*** current = self.head whilecurrent: 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.valueclassNode:  def \_\_init\_\_(self, value):  self.value= value
  self.next=None*# Testclasslinked 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
classlinked_list{constructor(){this.head=null;this.tail=null;}insert_node_at_start(value){let new_node =newNode(value); new_node.next=this.head;this.head= new_node;}insert_node_at_end(value){let new_node =newNode(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++;}returnconsole.log("Valor no encontrado\n");}delete_value(value){if(this.head==null){returnconsole.log("Empty list");}let current =this.head;while(current){if(current.next.value== value){ current.next= current.next.next;return;} current = current.next;}returnconsole.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;}returnconsole.log("Head: ", previous.value);}odd_even(){if(this.head==null||this.head.next==null){returnthis.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;}}classNode{constructor(value){this.value= value;this.next=null;}}const llist =newlinked_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();```classlinked\_list{constructor(){*this*.head=null;*this*.tail=null;}  insert\_node\_at\_start(value){letnew\_node =newNode(value);new\_node.next=*this*.head;*this*.head=new\_node;}  insert\_node\_at\_end(value){letnew\_node =newNode(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++;}returnconsole.log("Valor no encontrado\n");} delete\_value(value){if(*this*.head==null){returnconsole.log("Empty list");}let current =*this*.head;while(current){if(current.next.value== value){ current.next= current.next.next;return;} current = current.next;}returnconsole.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;}}classNode{constructor(value){*this*.value= value;*this*.next=null;}}const llist =newlinked\_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();
defoddeven(self): counter =0 current = self.head
even_head =None even_tail =None odd_head =None odd_tail =Nonewhile current isnotNone: counter +=1if counter %2==0:# Check if the index is evenif even_head isNone: even_head = current
even_tail = current
else: even_tail.next= current
even_tail = even_tail.nextelse:if odd_head isNone: odd_head = current
odd_tail = current
else: odd_tail.next= current
odd_tail = odd_tail.next current = current.next# Connect the even and odd listsif even_head isnotNone: even_tail.next=Noneif odd_head isnotNone: odd_tail.next= even_head
return odd_head
Mi respuesta:
Esta es mi solución:
Pasaron las pruebas, fue de gran ayuda el Playground