We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
Reverse a Circular Linked List
Last updated:
Reversing a circular linked list changes the direction of the ring: after the reversal, following next visits the nodes in the opposite order, and the old last node becomes the new head.
The method is the same three-pointer technique used for a singly linked list, with two changes: the loop stops when it comes back to the head instead of at null, and at the end the old head (now the last node) must be linked to the new head so that the list stays circular. It takes O(n) time and O(1) extra space.
Example:
Java program implementation of reverse a circular linked list
ReverseCLL.java
// Class representing a node in a circular linked list
class Node {
public int data; // Store value of the node
public Node next; // Store address of the next node
// Constructor to initialize a node
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class ReverseCLL {
// Function to reverse the circular linked list
// Reverses the ring and returns the new head
static Node reverse(Node head) {
if (head == null) {
return null;
}
Node prev = null;
Node current = head;
Node next;
// Turn every next link around, going once round the ring
do {
next = current.next;
current.next = prev;
prev = current;
current = next;
} while (current != head);
// The old head is now the last node: close the ring
head.next = prev;
// prev is the old last node, which is the new head
return prev;
}
static void printList(Node head) {
if (head == null)
return;
Node temp = head;
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != head);
}
public static void main(String args[]) {
// Create a circular linked list
Node head = new Node(65);
head.next = new Node(47);
head.next.next = new Node(32);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(52);
head.next.next.next.next.next = head;
System.out.print("Original circular linked list: ");
printList(head);
head = reverse(head);
System.out.print("\nReversed circular linked list: ");
printList(head);
}
}
Time Complexity: O(n), where n is the number of nodes.
Extra Space: O(1)
Frequently Asked Questions (FAQ)
Q1. How do you reverse a circular linked list?
Reverse every next link as in a linear list, but stop when you return to the head. Finally, link the old head to the new head to close the ring:
ReverseCircular.java
public class ReverseCircular {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static Node reverse(Node head) {
if (head == null || head.next == head) {
return head;
}
Node previous = null;
Node current = head;
do {
Node next = current.next;
current.next = previous;
previous = current;
current = next;
} while (current != head);
head.next = previous; // old head (now last) points to the new head
return previous; // previous is the new head
}
}
Q2. Why is the final step head.next = previous needed?
During the loop, the old head's next is set to null, because previous was null at the first step. The old head is now the last node, so it must point to the new head to keep the list circular.
Q3. What is the time and space complexity of reversing a circular list?
O(n) time, because each node is visited once, and O(1) extra space.
Q4. What are the edge cases?
An empty list stays empty.
A list with one node (pointing to itself) stays the same.
A list with two nodes ends up with the head and second node swapped but still pointing to each other.
Q5. How do you verify that a reversed circular list is correct?
Print it with a do-while loop from the new head. The values should appear in reverse order, and the traversal should return to the head after exactly n steps.
Q6. Can you reverse a circular linked list using a stack?
Yes. Push all values onto a stack, then walk the list again and overwrite each node's value with the popped value. It is easy to write but needs O(n) extra space:
ReverseWithStack.java
import java.util.ArrayDeque;
import java.util.Deque;
public class ReverseWithStack {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static void reverseValues(Node head) {
if (head == null) {
return;
}
Deque<Integer> stack = new ArrayDeque<>();
Node current = head;
do {
stack.push(current.data);
current = current.next;
} while (current != head);
do {
current.data = stack.pop();
current = current.next;
} while (current != head);
}
}
Q7. How do you reverse a circular doubly linked list?
Swap the prev and next references of every node, going around once, and make the old last node (head.prev before the swap) the new head.
Q8. How does reversing a circular list differ from reversing a linear list?
The loop condition changes from current != null to "until we are back at the head", and one extra link must be fixed at the end to close the ring.
Q9. If the list is tracked by a last reference, what changes after reversing?
The old head becomes the new last node, so the last reference must be updated to point to the old head.
Q10. Write a recursive Java method to reverse a circular linked list.
ReverseRecursiveCircular.java
public class ReverseRecursiveCircular {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static Node reverse(Node head) {
if (head == null || head.next == head) {
return head;
}
Node newHead = reverseFrom(head, head, null);
head.next = newHead; // close the ring
return newHead;
}
private static Node reverseFrom(Node current, Node head, Node previous) {
Node next = current.next;
current.next = previous;
if (next == head) {
return current; // the last original node is the new head
}
return reverseFrom(next, head, current);
}
}