Java Program for Reverse a linked list
Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples:
Input: Head of following linked list
1->2->3->4->NULL
Output: Linked list should be changed to,
4->3->2->1->NULL
Input: Head of following linked list
1->2->3->4->5->NULL
Output: Linked list should be changed to,
5->4->3->2->1->NULL
Input: NULL
Output: NULL
Input: 1->NULL
Output: 1->NULL
Iterative Method:
Java
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node( int d) {
data = d;
next = null ;
}
}
Node reverse(Node node) {
Node prev = null ;
Node current = node;
Node next = null ;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
void printList(Node node) {
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node( 85 );
list.head.next = new Node( 15 );
list.head.next.next = new Node( 4 );
list.head.next.next.next = new Node( 20 );
System.out.println( "Given Linked list" );
list.printList(head);
head = list.reverse(head);
System.out.println( "" );
System.out.println( "Reversed linked list " );
list.printList(head);
}
}
|
Output
Given Linked list
85 15 4 20
Reversed linked list
20 4 15 85
Time Complexity: O(N), where N is the length of the given linked list
Auxiliary Space: O(1)
A Simpler and Tail Recursive Method:
Java
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node( int d) {
data = d;
next = null ;
}
}
Node reverseUtil(Node curr, Node prev) {
if (curr.next == null ) {
head = curr;
curr.next = prev;
return null ;
}
Node next1 = curr.next;
curr.next = prev;
reverseUtil(next1, curr);
return head;
}
void printList(Node node) {
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.head = new Node( 1 );
list.head.next = new Node( 2 );
list.head.next.next = new Node( 3 );
list.head.next.next.next = new Node( 4 );
list.head.next.next.next.next = new Node( 5 );
list.head.next.next.next.next.next = new Node( 6 );
list.head.next.next.next.next.next.next = new Node( 7 );
list.head.next.next.next.next.next.next.next = new Node( 8 );
System.out.println( "Original Linked list " );
list.printList(head);
Node res = list.reverseUtil(head, null );
System.out.println( "" );
System.out.println( "" );
System.out.println( "Reversed linked list " );
list.printList(res);
}
}
|
Output
Original Linked list
1 2 3 4 5 6 7 8
Reversed linked list
8 7 6 5 4 3 2 1
Time Complexity: O(N), where N is the length of the given linked list
Auxiliary Space: O(1)
Please refer complete article on Reverse a linked list for more details!
Last Updated :
17 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...