class Node{
int value;
Node next;
Node(int number){
number = value;
next = null;
}
}
class linkList {
Node head;
public linkList()
{
head = null;
}//constructor
public void reverse() {
// TODO Auto-generated method stub
if (head == null || head.next == null)
return;
Node current = head;
Node previous = null;
Node next = null;
while(current != null){
next = current.next;
current.next = previous; // make current point to null
previous = current; // make previous to be the last node
current = next; // move current to next place
}
head = previous;
}
public void reverse_rc(Node rest2){
Node first;
Node rest;
first = head;
rest = head.next;
if(rest == null)
return ;
reverse_rc(rest);
first.next.next = first;
first.next = null;
head = rest;
}
private void printV() {
// TODO Auto-generated method stub
Node current = head;
while(current != null){
System.out.print(current.value + " ");
current = current.next ;
}
}
public static void main(String args[])
{
linkList list = new linkList();
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = new Node(3);
Node n4 = new Node(4);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n1.value = 1;
n2.value = 2;
n3.value = 3;
n4.value = 4;
list.head = n1;
list.printV();
list.reverse();
System.out.println(" ");
list.printV();
}
}