How to invert a simple chained list in Java? For example, I have the following values: 3 - 2 - 1 and the end result will have to be 1 - 2 - 3.
Note: without using native Java functions such as ArrayList.
public void inverteLista() {
Lista atual = new Lista();
atual.setProx(primeiro);
for (int i = 0; i < tamanho; i++) {
atual = atual.getProx();
System.out.println(atual.getValor());
}
}
This code prints the list in normal order, however I do not know the logic used to reverse it.