Может ли кто-нибудь сказать мне, почему мой код работает? Я хочу изменить один связанный список в java: Это метод (который не работает правильно)
public void reverseList(){
Node before = null;
Node tmp = head;
Node next = tmp.next;
while(tmp != null){
if(next == null)
return;
tmp.next = before;
before = tmp;
tmp = next;
next = next.next;
}
}
И это класс Node:
public class Node{
public int data;
public Node next;
public Node(int data, Node next){
this.data = data;
this.next = next;
}
}
На входе 4- > 3- > 2- > 1 я получил выход 4. Я отлаживал его, и он правильно устанавливает указатели, но все же я не понимаю, почему он выводит только 4.