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
class Node {
Node next;
int value;
public Node() {
}
public Node(Node next, int value) {
super();
this.next = next;
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
public class Linkedlist {
private Node head = null;
public Linkedlist(Node head) {
this.head = head;
}
public void iterate() {
Node current = head;
while (current != null) {
System.out.println(current.getValue());
current = current.getNext();
}
}
public void reverse() {
Node current = head;
Node prev = null;
while (current != null) {
Node temp = current.next;
current.next = prev;
prev = current;
current = temp;
}
head = prev;
}
public static void main(String[] args) {
Node n = new Node(null, 10);
Node n1 = new Node(n, 20);
Node n2 = new Node(n1, 30);
Node n3 = new Node(n2, 40);
Node n4 = new Node(n3, 50);
Node n5 = new Node(n4, 60);
Linkedlist linkedlist = new Linkedlist(n5);
linkedlist.iterate();
linkedlist.reverse();
System.out.println("------------------REVERSED---------------------");
linkedlist.iterate();
}
}
The corrected Code for reversing a Linked list without recursion.
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;
}
head = previous;
}
}
public class Main {
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();
}
}
Consider what the recursive method does when you're printing the links. It doesn't actually do anything until it has them all (i.e. it's placing each call on the stack, then when the base case is reached, it pops elements from the stack).
What non-stack structure do you have readily available that could be used to store the reverse of the list, then let you print it out?
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;
}
}
check your condition: while (isEmpty(head))
you forgot to add the "!" you meant while not empty... do