Linked List January 28 ,2026

Circular Linked List Traversal

Problem Statement

Given a circular linked list, write a program to traverse the list and print all its elements exactly once.

In a circular linked list, the last node does not point to NULL. Instead, it points back to the first node, forming a loop. Because of this, special care must be taken to avoid infinite traversal.

Example

Input

Circular Linked List:
10 → 20 → 30 → 40 → (back to 10)

Output

10 20 30 40

Approach

  1. Check if the circular linked list is empty.
    • If head == NULL, there are no nodes to traverse.
  2. Initialize a pointer current with the head node.
  3. Print the data of the current node.
  4. Move the pointer to the next node.
  5. Continue traversal until the pointer reaches the head node again.
  6. Stop the traversal once all nodes are visited exactly once.

A do-while loop is preferred because the head node must be processed at least once.

Algorithm

  1. If head is NULL, return.
  2. Set current = head.
  3. Repeat:
    • Print current.data
    • Move current = current.next
  4. Until current == head

Time and Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Where n is the number of nodes in the circular linked list.

Implementation

C Implementation

// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h"

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

void traverse(struct Node* head) {
    if (head == NULL)
        return;

    struct Node* current = head;

    do {
        printf("%d ", current->data);
        current = current->next;
    } while (current != head);
}

// Helper to create circular list: 10→20→30→40→head
struct Node* createList() {
    struct Node *head, *second, *third, *fourth;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));
    fourth = (struct Node*)malloc(sizeof(struct Node));

    head->data = 10; head->next = second;
    second->data = 20; second->next = third;
    third->data = 30; third->next = fourth;
    fourth->data = 40; fourth->next = head; // circular link

    return head;
}

int main() {
    struct Node* head = createList();

    traverse(head);

    return 0;
}

C++ Implementation

// While running this code, replace all #include "..." with #include <...>

#include "iostream"
#include "cstdlib"

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

void traverse(struct Node* head) {
    if (head == NULL)
        return;

    struct Node* current = head;

    do {
        printf("%d ", current->data);
        current = current->next;
    } while (current != head);
}

// Helper function
struct Node* createList() {
    struct Node *head, *second, *third, *fourth;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));
    fourth = (struct Node*)malloc(sizeof(struct Node));

    head->data = 10; head->next = second;
    second->data = 20; second->next = third;
    third->data = 30; third->next = fourth;
    fourth->data = 40; fourth->next = head;

    return head;
}

int main() {
    struct Node* head = createList();

    traverse(head);

    return 0;
}

Java Implementation

class Node {
    int data;
    Node next;

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

public class CircularLinkedListTraversal {

    static void traverse(Node head) {
        if (head == null)
            return;

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

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def traverse(head):
    if head is None:
        return

    current = head
    while True:
        print(current.data, end=" ")
        current = current.next
        if current == head:
            break

JavaScript Implementation

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

function traverse(head) {
    if (head === null) return;

    let current = head;
    do {
        console.log(current.data);
        current = current.next;
    } while (current !== head);
}

Output- 

10 20 30 40

Key Points to Remember

  • Circular linked lists do not have a NULL pointer
  • Traversal must stop when the head node is reached again
  • do-while loop is ideal for traversal
  • Always handle the empty list case

Conclusion

Circular Linked List Traversal requires a different stopping condition compared to linear linked lists. By checking when the traversal reaches the head node again, all elements can be visited exactly once without causing an infinite loop. This approach is efficient, easy to implement, and commonly asked in data structure interviews and exams.


Next Problem in the Series

 Check if a Linked List is Circular

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Remove Eve...
Linked List January 01 ,2026

Remove Every K-th No...

Middle of...
Linked List January 01 ,2026

Middle of a Linked L...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Singly to...
Linked List January 01 ,2026

Singly to Circular L...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Print a Si...
Linked List February 02 ,2026

Print a Singly Linke...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Deleting a...
Linked List February 02 ,2026

Deleting a Node at a...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Delete Fir...
Linked List February 02 ,2026

Delete First Node of...

Check if L...
Linked List February 02 ,2026

Check if Linked List...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Swap Nodes...
Linked List February 02 ,2026

Swap Nodes in Pairs

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com