public class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
public Node reverse(Node head) {
Node pre = null;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
How does this code flip in the while loop? I want to be more detailed. I debugged it several times and still don’t understand what’s going on.
It will be easier to understand the purpose if you refer to it. The most confusing part is to deal with it from right to left, because you have to save the things at the back first, otherwise they will be overwritten and lost.
Ps: It is recommended to learn more about linked lists first