Reversing a singly linked list means turning every next link around so that the last node becomes the head and the old head becomes the last node. No values are copied and no new nodes are created; only the links change.
Example with 5 nodes:
Input: 10 -> 20 -> 30 -> 40 -> 50
Output: 50 -> 40 -> 30 -> 20 -> 10
It can be done with a loop or with recursion:
- Iterative Approach
- Recursive Approach
Reverse a linked list using Iterative approach
Keep three references while walking the list: prevNode (the part already reversed), currNode (the node being processed) and nextNode (the rest of the list, saved before its link is overwritten). For each node, save currNode.next, point currNode.next back to prevNode, then move both references one step forward. When currNode becomes null, prevNode is the new head. This takes O(n) time and O(1) extra space.
public class Node {
int data;
Node next;
// Constructor
Node(int data) {
this.data = data;
this.next = null;
}
}
// Java program to reverse a linked list using Iterative approach
public class ReverseUsingIterativeApproachInSLL {
static Node reverseList(Node head) {
// Initialize three pointers: currNode, prevNode and nextNode
Node currNode = head, prevNode = null, nextNode;
while (currNode != null) {
nextNode = currNode.next;
currNode.next = prevNode;
// Move pointers one position ahead
prevNode = currNode;
currNode = nextNode;
}
// Return the head of reversed linked list
return prevNode;
}
// Method to print a linked list
static void printList(Node node) {
while (node != null) {
System.out.print(" " + node.data);
node = node.next;
}
}
public static void main(String[] args) {
// Create a linked list
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);
// Linked list will be created like this
// 10 -> 20 -> 30 -> 40 -> 50
System.out.print("Original Linked list:");
printList(head);
head = reverseList(head);
System.out.print("\nReversed Linked List:");
printList(head);
}
}
Original Linked list: 10 20 30 40 50 Reversed Linked List: 50 40 30 20 10
Reverse a linked list using Recursive approach
First reverse everything after the head with a recursive call; that call returns the new head (the old last node). The old second node is now the tail of the reversed part, so make it point back to the head (head.next.next = head) and end the list at the old head (head.next = null). An empty list or a single node is already reversed. This is O(n) time and O(n) stack space.
// Java program to reverse a linked list using Recursive approach
public class ReverseUsingRecursiveApproachInSLL {
static Node reverseList(Node head) {
// An empty list or a single node is already reversed
if (head == null || head.next == null)
return head;
// Reverse everything after the head; rest is the new head
Node rest = reverseList(head.next);
// The old second node is now the tail: link it back to head
// and make head the new last node
head.next.next = head;
head.next = null;
return rest;
}
// Print a linked list
static void printList(Node node) {
while (node != null) {
System.out.print(" " + node.data);
node = node.next;
}
}
public static void main(String[] args) {
// Create a linked list
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);
// Linked list will be created like this
// 10 -> 20 -> 30 -> 40 -> 50
System.out.print("Original Linked list:");
printList(head);
head = reverseList(head);
System.out.print("\nReversed Linked List:");
printList(head);
}
}
Original Linked list: 10 20 30 40 50 Reversed Linked List: 50 40 30 20 10