how to revers a linked list of integers in java code example
Example: revese the linked list java
Easiest way
public static LinkedList reverse(LinkedList head) {
LinkedList prevAddress = null;
LinkedList currentAddress = head;
LinkedList nextAddress = head.next;
while(nextAddress!=null) {
currentAddress.next = prevAddress;
prevAddress = currentAddress;
currentAddress = nextAddress;
nextAddress= currentAddress.next;
currentAddress.next = prevAddress;
}
head = currentAddress;
return head;
}
Just try to visualize its all about pointers game. :-)
NO GFG ANSWER