Traversal of Circular Linked List

Last Updated : 6 Jul, 2026

Given the head of a circular linked list, print the data of the nodes in the linked list starting from the head node, traversing the list exactly once.

Example:

Input:

blobid0_1756106050

Output: 1 7 8 10
Explanation: The traversal begins at the head node 1, then subsequent nodes 7, 8, and 10.

Input:

blobid2_1756106068

Output: 2 5 7 8 10
Explanation: The traversal begins at head node 2, then subsequent nodes 5, 7, 8, and 10.

Try It Yourself
redirect icon

[Expected Approach 1] Using Recursion - O(n) Time and O(n) Space:

To idea is to traverse a circular linked list recursively, we will start by printing the value of the current node. Then, recursively call the function to handle the next node. If the next node is the same as the head node, indicating that a full cycle has been completed, we will end the recursion call.

C++
#include <iostream>
using namespace std;

class Node {
  public:
    int data;
    Node *next;
    Node(int x) {
        data = x;
        next = nullptr;
    }
};

void print(Node *curr, Node *head) {

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

    cout << curr->data << " ";

    if (curr->next == head)
        return;

    print(curr->next, head);
}

void printList(Node *head){
    print(head, head);
}

int main() {
  
 	// Create a hard-coded linked list
	// 11 -> 2 -> 56 -> 12
    Node *head = new Node(11);
    head->next = new Node(2);
    head->next->next = new Node(56);
    head->next->next->next = new Node(12);

    head->next->next->next->next = head;

    printList(head);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
    
};

void printList(struct Node* curr, struct Node* head) {

    // return if list is empty
    if (head == NULL) return;
    
    printf("%d ", curr->data);
    
    if (curr->next == head)
        return;
        
    printList(curr->next, head);

}

struct Node* createNode(int data) {
    struct Node* new_node = 
    	(struct Node*)malloc(sizeof(struct Node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

int main() {
  	
  	// Create a hard-coded linked list
	// 11 -> 2 -> 56 -> 12
    struct Node* head = createNode(11);
    head->next = createNode(2);
    head->next->next = createNode(56);
    head->next->next->next = createNode(12);

    head->next->next->next->next = head;

    printList(head, head);

    return 0;
}
Java
class Node {
  public int data;
  public Node next;

  public Node(int x) {
      data = x;
      next = null;
  }
}

public class GFG {

  public static void print(Node curr, Node head) {

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

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

      if (curr.next == head)
          return;

      print(curr.next, head);
  }

  public static void printList(Node head){
      print(head, head);
  }

  public static void main(String[] args) {
    
       // Create a hard-coded linked list
       // 11 -> 2 -> 56 -> 12
       Node head = new Node(11);
       head.next = new Node(2);
       head.next.next = new Node(56);
       head.next.next.next = new Node(12);

       head.next.next.next.next = head;

       printList(head);
  }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None

def printRec(curr, head):

    # return if list is empty
    if head is None:
        return

    print(curr.data, end=' ')

    if curr.next == head:
        return

    printRec(curr.next, head)

def printList(head):
    printRec(head, head)

if __name__ == '__main__':
  
    # Create a hard-coded linked list
    # 11 -> 2 -> 56 -> 12
    head = Node(11)
    head.next = Node(2)
    head.next.next = Node(56)
    head.next.next.next = Node(12)

    head.next.next.next.next = head

    printList(head)
C#
using System;

public class Node {
  public int data;
  public Node next;

  public Node(int x) {
      data = x;
      next = null;
  }
}

public class GFG {

  public static void print(Node curr, Node head) {

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

      Console.Write(curr.data + " ");

      if (curr.next == head)
          return;

      print(curr.next, head);
  }

  public static void printList(Node head){
      print(head, head);
  }

  public static void Main() {
    
      // Create a hard-coded linked list
      // 11 -> 2 -> 56 -> 12
      Node head = new Node(11);
      head.next = new Node(2);
      head.next.next = new Node(56);
      head.next.next.next = new Node(12);

      head.next.next.next.next = head;

      printList(head);
  }
}
JavaScript
class Node {
  constructor(x) {
    this.data = x;
    this.next = null;
  }
}

function printRec(curr, head) {

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

  process.stdout.write(curr.data + ' ');

  if (curr.next === head)
    return;

  printRec(curr.next, head);
}

function printList(head){
  printRec(head, head);
}

// Create a hard-coded linked list
// 11 -> 2 -> 56 -> 12
let head = new Node(11);
head.next = new Node(2);
head.next.next = new Node(56);
head.next.next.next = new Node(12);

head.next.next.next.next = head;

printList(head);

Output
11 2 56 12 

[Expected Approach 2] Using Iterative Method - O(n) Time and O(1) Space:

To idea is to traverse a circular linked list iteratively, starting at the head node and repeatedly printing the value of the current node while moving to its next node. Continue this process until we return back to the head node, indicating that the full cycle of the circular linked list has been completed.

C++
#include <iostream>
using namespace std;

class Node {
  public:
    int data;
    Node *next;
    Node(int x) {
        data = x;
        next = nullptr;
    }
};

void printList(Node *head) {

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

    // initialize current node as head
    Node* curr = head;

    // loop through the circular linked list
    do {

        // print the data of current node
        cout << curr->data << " ";

        // move to the next node
        curr = curr->next;

    } while (curr != head);
}

int main() {
  	
  	// Create a hard-coded linked list
	// 11 -> 2 -> 56 -> 12
    Node *head = new Node(11);
    head->next = new Node(2);
    head->next->next = new Node(56);
    head->next->next->next = new Node(12);

    head->next->next->next->next = head;

    printList(head);

    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
    
};

void printList(struct Node* head) {

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

    struct Node* curr = head;
    do {
        printf("%d ", curr->data);
        curr = curr->next;

    } while (curr != head);
    printf("\n");
}

struct Node* createNode(int data) {
    struct Node* new_node = 
   	 (struct Node*)malloc(sizeof(struct Node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

int main() {
  
  	// Create a hard-coded linked list
	// 11 -> 2 -> 56 -> 12
    struct Node* head = createNode(11);
    head->next = createNode(2);
    head->next->next = createNode(56);
    head->next->next->next = createNode(12);

    head->next->next->next->next = head;

    printList(head);

    return 0;
}
Java
import java.io.*;

class Node {
    public int data;
    public Node next;

    public Node(int x) {
        data = x;
        next = null;
    }
}

public class GFG {

    public static void printList(Node head) {

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

        // initialize current node as head
        Node curr = head;

        // loop through the circular linked list
        do {

            // print the data of current node
            System.out.print(curr.data + " ");

            // move to the next node
            curr = curr.next;

        } while (curr!= head);
    }

    public static void main(String[] args) {

        // Create a hard-coded linked list
        // 11 -> 2 -> 56 -> 12
        Node head = new Node(11);
        head.next = new Node(2);
        head.next.next = new Node(56);
        head.next.next.next = new Node(12);

        head.next.next.next.next = head;

        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None

def printList(head):

    # return if list is empty
    if head is None:
        return

    # initialize current node as head
    curr = head

    # loop through the circular linked list
    while True:

        # print the data of current node
        print(curr.data, end=' ')

        # move to the next node
        curr = curr.next

        if curr == head:
            break

if __name__ == '__main__':

    # Create a hard-coded linked list
    # 11 -> 2 -> 56 -> 12
    head = Node(11)
    head.next = Node(2)
    head.next.next = Node(56)
    head.next.next.next = Node(12)

    head.next.next.next.next = head

    printList(head)
C#
using System;

public class Node
{
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG
{
    public static void printList(Node head)
    {
        // return if list is empty
        if (head == null)
            return;

        // initialize current node as head
        Node curr = head;

        // loop through the circular linked list
        do
        {
            // print the data of current node
            Console.Write(curr.data + " ");

            // move to the next node
            curr = curr.next;
        }
        while (curr!= head);
    }

    public static void Main()
    {
        // Create a hard-coded linked list
        // 11 -> 2 -> 56 -> 12
        Node head = new Node(11);
        head.next = new Node(2);
        head.next.next = new Node(56);
        head.next.next.next = new Node(12);

        head.next.next.next.next = head;

        printList(head);
    }
}
JavaScript
class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
    }
}

function printList(head) {

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

    // initialize current node as head
    let curr = head;

    // loop through the circular linked list
    do {

        // print the data of current node
        process.stdout.write(curr.data + ' ');

        // move to the next node
        curr = curr.next;

    } while (curr!== head);
}

// Create a hard-coded linked list
// 11 -> 2 -> 56 -> 12
let head = new Node(11);
head.next = new Node(2);
head.next.next = new Node(56);
head.next.next.next = new Node(12);

head.next.next.next.next = head;

printList(head);

Output
11 2 56 12 

Related articles:

Comment