00:00

Traversal of Circular Linked List

Last updated:

To traverse a circular linked list is to visit every node exactly once, going round the ring from a starting node until you arrive back at it. The key difference from a linear list is the stop condition: there is no null at the end, so the loop must stop when the current node is the starting node again.

The traversal can be written in two ways:

  1. Iterative Approach
  2. Recursive Approach

1. Traverse a Circular Linked List using Iterative Approach

A do-while loop prints the current node first and only then checks whether it has come back to the head. A plain while (currNode != head) loop would stop before printing anything, because the condition is false at the start.

TraversalUsingIterative.java
// A Java program to traverse a circular linked list using Iterative approach
public class TraversalUsingIterative {
  static class Node {
    int data;
    Node next;

    Node(int data) {
      this.data = data;
    }
  }

  // Function to traverse a list using Iterative approach
  static void printList(Node head) {
    // return if list is empty
    if (head == null)
      return;

      Node currNode = head;
    do {
      System.out.print(currNode.data + " ");
      currNode = currNode.next;

    } while (currNode != head);
    System.out.println();
  }

  public static void main(String[] args) {
    // Create a circular linked list
    Node head = new Node(50);
    head.next = new Node(20);
    head.next.next = new Node(40);
    head.next.next.next = new Node(10);
    head.next.next.next.next = new Node(30);
    head.next.next.next.next.next = head;

    System.out.println("Traversing a circular linked list:");
    printList(head);
  }
}
Output
Traversing a circular linked list:
50 20 40 10 30

Time Complexity: O(n), where n is the number of nodes in the ring.

Extra Space: O(1)

2. Traverse a Circular Linked List using Recursive Approach

The method prints one node and calls itself on the next node, passing the head along so that every call knows where the ring started. The recursion stops when the next node is the head.

TraversalUsingRecursive.java
// A Java program to traverse a circular linked list using Recursive approach
public class TraversalUsingRecursive {
  static class Node {
    int data;
    Node next;

    Node(int data) {
      this.data = data;
    }
  }

  static void printList(Node curr, Node head) {

    // return if list is empty
    if (head == null)
      return;

    System.out.print(curr.data + " ");

    if (curr.next == head)
      return;

    printList(curr.next, head);
  }

  public static void main(String[] args) {
    // Create a circular linked list
    Node head = new Node(67);
    head.next = new Node(42);
    head.next.next = new Node(81);
    head.next.next.next = new Node(10);
    head.next.next.next.next = new Node(58);
    head.next.next.next.next.next = head;

    System.out.println("Traversing a circular linked list:");
    printList(head, head);
  }
}
Output
Traversing a circular linked list:
67 42 81 10 58

Time Complexity: O(n), where n is the number of nodes in the ring.

Extra Space: O(n), one stack frame per node.