Reverse a single linked list with a while loop in Java
1. Create two Node references, the first Node reference stores the prevous node and the second Node reference stores the current node.
2. Store the current next Node reference in a tmp, then switch the previous and current nodes.
3. Iterate the current Node to the next Node.
Node reverseLinkedList(Node node){
Node prev = null;
Node curr = node;
while(curr != null){
// keep the current node in order to iterate it late on
Node nnext = curr.next;
curr.next = prev;
prev = curr;
curr = nnext;
}
return prev;
}
Reverse Single LinkedList
Simple, elegant but it is hard to get it right in the first shot