Skip to content

Instantly share code, notes, and snippets.

@munguial
Created September 26, 2023 04:52
Show Gist options
  • Save munguial/cb141a57e26754900e7666f0498bdc65 to your computer and use it in GitHub Desktop.
Save munguial/cb141a57e26754900e7666f0498bdc65 to your computer and use it in GitHub Desktop.
public class List {
private Node start, end;
private int size;
public void add(Object variable){
if(start==null){
start = end = new Node(variable);
}
else{
Node temporary = new Node(variable);
end.next = temporary;
end = end.next;
}
size++;
}
public int size(){
return size;
}
public String printList(){
String storing = "List ("+size()+") {";
Node reference = start;
while(reference!=null){
storing += reference.getVariable()+", ";
reference = reference.next;
}
return storing.substring(0, storing.length()-2)+"}";
}
public Node getStartNode(){
return start;
}
public Node getNextNode(){
Node temporary = start.next;
start=start.next;
return temporary;
}
public Node reverseLinkedList(Node head){
if(head.next==null){
start = new Node(head.getVariable());
size++;
return start;
}
else{
while(head!=null){
if(start==null){
start = new Node(head.getVariable());
size++;
head=head.next;
}
else{
Node temporary = new Node(head.getVariable());
temporary.next=start;
start=temporary;
head=head.next;
size++;
}
}
}
return start;
}
///////////Class Node//////////////////////////////////////
private class Node{
private Object variable;
private Node next;
public Node(Object variable){
this.variable=variable;
next = null;
}
public Object getVariable(){
return variable;
}
}
////////////////////////////////////////////////////////
public static void main(String[] args) {
List list = new List();
List secondList = new List();
list.add("David");
list.add(19);
list.add("Soccer");
list.add(9);
list.add(3);
System.out.println(list.printList());
System.out.println("");
System.out.println("New start node: "+secondList.reverseLinkedList(list.getStartNode()).getVariable());
System.out.println("Reverse list: "+secondList.printList());
}
}
@munguial
Copy link
Author

Hola David, ya revisé tu código, me parece genial que hayas encontrado la manera de resolver el problema con el conocimiento que obtuviste, además de que veo que cambiaste todos los nombres de variable a inglés, muy bien!.

Hay algunos puntos que me gustaría que modificaras para mejorar tu código:

  1. Actualmente tu solución crea una nueva lista, es decir, crea nuevos nodos y le asigna los valores de los nodos existentes, de tal manera que al final de tu método tenemos N * 2 nodos. Digamos que ahora tenemos la restricción de que tu solución no puede utilizar memoria extra, es decir, no puedes crear nuevos objetos, tu método debe recibir la lista de nodos y modificar las referencias de tal manera que los objetos existentes sean los mismos en la lista que vas a regresar como respuesta.

  2. La variable size está siendo actualizada en el método reverseLinkedList, me parece que esto no es necesario, creo que podrías eliminar esas líneas de código donde modificas la variable size.

  3. Algo que me parece un poco confuso es la variable "head". En este caso estás usando head para recorrer la lista, yo creo que podríamos renombrar esa variable a algo más preciso como "iterator" o "cursor" o algo que indique que es una referencia cuya intención es iterar sobre la lista, al llamar esta variable "head" me hace pensar que siempre tiene que apuntar al primer elemento de la lista.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment