00:00

Circular Linked List Data Structures

Last updated:

A circular linked list is a linked list whose last node links back to the first node, so the nodes form a ring instead of a line. This page covers its two types, its operations and their cost, a Java implementation, and when a circular list is a good fit.

What is a circular linked list?

In a circular linked list, the last node's next refers to the first node instead of being null. There is no natural end: starting from any node and following next, you eventually come back to the same node.

A circular linked list of 20, 40, 60 and 80 Four nodes, 20, 40, 60 and 80, are linked in a ring. Head is 20 and tail is 80. The next link of 80 points back to 20, so a pointer that keeps following next visits 20, 40, 60, 80 and then 20 again; there is no null. 20406080headtail80.next → 20No null at the endlast node links to first A circular linked list of 20, 40, 60 and 80 Four nodes, 20, 40, 60 and 80, are linked in a ring. Head is 20 and tail is 80. The next link of 80 points back to 20, so a pointer that keeps following next visits 20, 40, 60, 80 and then 20 again; there is no null. 20406080headtail80.next → 20
Following next from any node visits every node and comes back to where it started: 20 → 40 → 60 → 80 → 20 → …

Because there is no null to stop at, loops over a circular list must stop when they return to the node where they began. Like other linked lists, the ring grows and shrinks one node at a time.

Types of Circular Linked Lists

Either kind of linked list can be closed into a ring, which gives two types:

1. Circular Singly Linked List

A singly linked list whose last node points back to the first. Each node has only a next link, so the ring can be walked in one direction only.

Circular singly linked list Each node has a data field and a next field. 20 points to 40, 40 to 60, 60 to 80, and the next field of 80, the tail, points back to 20, the head. Traversal moves forward only. 20datanext40datanext60datanext80datanextheadtailtail.next points back to head (no null)forward only → Circular singly linked listNodes 20, 40, 60 and 80 are stacked. Each next field points to the node below, and the next field of 80, the tail, points back up to 20, the head.20406080datanextheadtailtail.next → head
Circular singly linked list: every node has data and next; tail.next is head, so traversal only moves forward and wraps around.

2. Circular Doubly Linked List

A doubly linked list closed at both ends: the last node's next refers to the first node, and the first node's prev refers to the last node. No link is null, and the ring can be walked in both directions.

Circular doubly linked list Each node has prev, data and next fields. Next links go 20 to 40 to 60 to 80, and the next of 80 goes back to 20. Prev links go 80 to 60 to 40 to 20, and the prev of 20 goes back to 80. One pointer moves forward along next links while another moves backward along prev links. 20prevdatanext40prevdatanext60prevdatanext80prevdatanexttail.next = headhead.prev = tailheadtailnext (forward)prev (backward) Circular doubly linked listNodes 20, 40, 60 and 80 are stacked, each with prev, data and next. Next links go down the right side and the next of 80 wraps back up to 20. Prev links go up the left side and the prev of 20 wraps down to 80.20406080prevdatanextheadtailtail.next = headhead.prev = tailnextprev
Circular doubly linked list: tail.next is head and head.prev is tail, so you can loop forward (purple) or backward (teal) without ever reaching null.

Operations on the Circular Linked List

We can perform the following operations on the Circular Linked List as given below.

  • Insertion
    • Insert node at the beginning
    • Insert node at the end
    • Insert node at the specific position
  • Deletion
    • Delete the first node
    • Delete the last node
    • Delete node from the specific position
  • Searching
    • Search a specific node

Time and Space Complexity of Circular Linked List Operations

OperationsTime ComplexitySpace Complexity
InsertionO(1) to O(n)O(1)
DeletionO(1) to O(n)O(1)
TraversalO(n)O(1)
SearchO(n)O(1)
AccessO(n)O(1)

Example of Circular Linked List in Java

Now, let's understand the implementation of circular linked list program in Java.

Class: Node

Node.java
class Node {
  int data;       // Store value
  Node next;      // Store address of next node

  // Initializing a node with data in the constructor
  public Node(int data) {
    this.data = data;
    this.next = null;
  }
}

Class: CircularLinkedList

CircularLinkedList.java
class CircularLinkedList {
  private Node head;
  private Node tail;

  // Setting null in head and tail in the constructor
  public CircularLinkedList() {
    head = null;
    tail = null;
  }

  // Method to insert a new node into the circular linked list
  public void insertNode(int data) {
    Node newNode = new Node(data);

    // If the list is empty, make the new node the head and tail
    if (head == null) {
      head = newNode;
      tail = newNode;

      // Point to itself in a circular list
      newNode.next = head;
    } else {
      newNode.next = head;
      tail.next = newNode;
      tail = newNode;
    }
  }

  // Method to delete an existing node from the circular linked list
  public void deleteNode(int key) {
    if (head == null) {
      System.out.println("List is empty");
      return;
    }

    // prev starts at the tail, the node before head in the circle
    Node prev = tail;
    Node curr = head;

    do {
      if (curr.data == key) {
        if (head == tail) {
          // The only node: the list becomes empty
          head = null;
          tail = null;
        } else {
          prev.next = curr.next;   // skip the node
          if (curr == head) {
            head = curr.next;      // tail.next is now the new head
          }
          if (curr == tail) {
            tail = prev;           // prev.next is still head
          }
        }
        System.out.println("Given key " + key + " deleted successfully.");
        return;
      }
      prev = curr;
      curr = curr.next;
    } while (curr != head);

    System.out.println("Key not found in the list");
  }

  public void printList() {
    if (head == null) {
      System.out.println("List is empty");
      return;
    }

    System.out.print("Linked List Elements: ");

    Node itr = head;
    do {
      System.out.print(itr.data + " ");
      itr = itr.next;
    } while (itr != head);
    System.out.println();
  }
}

Class: CircularLinkedListOutput

CircularLinkedListOutput.java
public class CircularLinkedListOutput {
  public static void main(String[] args) {
    // Create a circular linked list
    CircularLinkedList list = new CircularLinkedList();

    // Insert elements into the list
    list.insertNode(20);
    list.insertNode(40);
    list.insertNode(60);
    list.insertNode(80);

    // Print the list
    list.printList();

    // Delete an element from the list
    list.deleteNode(60);

    // Delete a non existing element from the list
    list.deleteNode(10);

    // Print the list
    list.printList();
  }
}
Output
Linked List Elements: 20 40 60 80
Given key 60 deleted successfully.
Key not found in the list
Linked List Elements: 20 40 80

When to Use a Circular Linked List?

A circular list fits problems where the items naturally repeat in a cycle:

  • Taking turns: round-robin scheduling or players in a game, where after the last item you go back to the first.
  • Repeating sequences: a playlist on repeat or a slideshow that loops.
  • A queue that only needs one reference: keeping a reference to the last node gives O(1) access to both ends, because the first node is last.next.

Advantages of Circular Linked List

  • Natural cycling: moving past the last node brings you back to the first with no special case.
  • Any node can be a starting point: a full traversal can begin anywhere in the ring.
  • O(1) access to both ends with one reference: with a reference to the last node, inserting at the front or the back is O(1).
  • No fixed capacity: nodes are added and removed one at a time.

Disadvantages of Circular Linked List

  • Risk of infinite loops: a loop that waits for null never ends; the stop condition must be "back at the start".
  • More edge cases: the single-node ring (a node pointing to itself) and the head/tail links must be handled carefully on every insert and delete.
  • No clear end: it is a poor fit when the data has a natural first and last element.
  • Same access costs as other linked lists: no random access, and in a singly circular list deleting the last node is O(n).

Use Cases of Circular Linked List

  • Round-robin CPU or task scheduling.
  • Media players that loop a playlist.
  • Turn order in multiplayer games.
  • The Josephus problem and other "every k-th item" puzzles (see FAQ Q7).

Choose a circular linked list when the problem itself is circular. When the data has a clear beginning and end, an ordinary linked list or an array is simpler and less error-prone.