Problem in Reversing a linked list without using recursion.
I used this method, but when i try and run this back home, I am not able to print the reverse of the lin
There are two problems in your code. One: you check for isEmpty(head) where as you should check for !isEmpty(head). Second: when you fix first problem, then 'head' becomes null when loop terminates.
The correct code with fix for above two problems:
void reverse() {
link previous = null, temp = null;
while (!isEmpty(head)) {
temp = head.nextlink;
head.nextlink = previous;
previous = head;
if (temp == null) {
break;
}
head = temp;
}
}