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 linked list even though the function looks good It goes on to print the linked list in the same way as it did earlier.
Can someone help me understand what is wrong here??
class link {
int data;
public link nextlink;
link(int d1) {
data = d1;
}
}
class List{
link head;
link revhead;
List(){
head = null;
}
boolean isEmpty(link head) {
return head==null;
}
void insert(int d1) {
link templink = new link(d1);
templink.nextlink = head;
head = templink;
}
void printlist(){
link head1 = head;
while(!isEmpty(head1)) {
System.out.print(head1.data + " ");
head1 = head1.nextlink;
}
System.out.println();
}
void reverse() {
link previous=null,temp=null;
while(isEmpty(head)) {
temp = head.nextlink;
head.nextlink = previous;
previous = head;
head = temp;
}
}
}
public class LinkedList {
public static void main(String[] args) {
List list1 = new List();
list1.insert(10);
list1.insert(20);
list1.insert(30);
list1.insert(40);
list1.insert(50);
list1.printlist();
list1.reverse();
list1.printlist();
}
}
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: