Java Program to Reverse Linked List with Output
next pointer to the previous node, like this: while (current != null) { Node next = current.next; current.next = prev; prev = current; current = next; }.Examples
How to Think About It
Algorithm
Code
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class ReverseLinkedList { static Node reverse(Node head) { Node prev = null; Node current = head; while (current != null) { Node next = current.next; current.next = prev; prev = current; current = next; } return prev; } static void printList(Node head) { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("null"); } public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); System.out.print("Original list: "); printList(head); head = reverse(head); System.out.print("Reversed list: "); printList(head); } }
Dry Run
Let's trace the list 1 -> 2 -> 3 -> null through the reverse function.
Initialize pointers
prev = null, current = Node(1)
First iteration
next = Node(2), current.next = prev (null), prev = Node(1), current = Node(2)
Second iteration
next = Node(3), current.next = prev (Node(1)), prev = Node(2), current = Node(3)
Third iteration
next = null, current.next = prev (Node(2)), prev = Node(3), current = null
End loop
current is null, return prev as new head (Node(3))
| prev | current | next | current.next |
|---|---|---|---|
| null | Node(1) | Node(2) | null |
| Node(1) | Node(2) | Node(3) | Node(1) |
| Node(2) | Node(3) | null | Node(2) |
Why This Works
Step 1: Track previous and current nodes
We keep a prev pointer to remember the reversed part and a current pointer to process nodes one by one.
Step 2: Reverse the link
For each node, we change its next pointer to point to prev, flipping the direction.
Step 3: Move forward
We move prev and current forward to continue reversing until all nodes are processed.
Alternative Approaches
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class ReverseLinkedList { static Node reverseRecursive(Node head) { if (head == null || head.next == null) return head; Node rest = reverseRecursive(head.next); head.next.next = head; head.next = null; return rest; } static void printList(Node head) { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("null"); } public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); System.out.print("Original list: "); printList(head); head = reverseRecursive(head); System.out.print("Reversed list: "); printList(head); } }
import java.util.Stack; class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } public class ReverseLinkedList { static Node reverseWithStack(Node head) { if (head == null) return null; Stack<Node> stack = new Stack<>(); Node temp = head; while (temp != null) { stack.push(temp); temp = temp.next; } Node newHead = stack.pop(); temp = newHead; while (!stack.isEmpty()) { temp.next = stack.pop(); temp = temp.next; } temp.next = null; return newHead; } static void printList(Node head) { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.println("null"); } public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); System.out.print("Original list: "); printList(head); head = reverseWithStack(head); System.out.print("Reversed list: "); printList(head); } }
Complexity: O(n) time, O(1) space
Time Complexity
The algorithm visits each node once, so the time is linear in the number of nodes, O(n).
Space Complexity
It uses only a few pointers and reverses the list in place, so space is O(1).
Which Approach is Fastest?
The iterative method is fastest and uses constant space, while recursion and stack methods use extra memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative reversal | O(n) | O(1) | Efficient in-place reversal |
| Recursive reversal | O(n) | O(n) | Cleaner code but uses stack memory |
| Using stack | O(n) | O(n) | Easy to understand but uses extra memory |