1class LinkedList {
2
3 static Node head;
4
5 static class Node {
6
7 int data;
8 Node next;
9
10 Node(int d)
11 {
12 data = d;
13 next = null;
14 }
15 }
16
17 /* Function to reverse the linked list */
18 Node reverse(Node node)
19 {
20 Node prev = null;
21 Node current = node;
22 Node next = null;
23 while (current != null) {
24 next = current.next;
25 current.next = prev;
26 prev = current;
27 current = next;
28 }
29 node = prev;
30 return node;
31 }
32
33 // prints content of double linked list
34 void printList(Node node)
35 {
36 while (node != null) {
37 System.out.print(node.data + " ");
38 node = node.next;
39 }
40 }
41
42 public static void main(String[] args)
43 {
44 LinkedList list = new LinkedList();
45 list.head = new Node(85);
46 list.head.next = new Node(15);
47 list.head.next.next = new Node(4);
48 list.head.next.next.next = new Node(20);
49
50 System.out.println("Given Linked list");
51 list.printList(head);
52 head = list.reverse(head);
53 System.out.println("");
54 System.out.println("Reversed linked list ");
55 list.printList(head);
56 }
57}