I was reversing a singly linked list using the following code:
Node prevNode = null;
Node currNode = head;
while (currNode != null) {
Node next = currNode.next;
currNode.next = prevNode;
prevNode = currNode;
currNode = next;
}
head = prevNode;
Then when I was going through some material about stacks it suggested that reversing any list is best done by using a stack.
It is for sure very easy to visualize that we indeed pop out the elements and push them into a new stack and in the end it is essentially reversed. But which of the two methods is better or would take lesser time?