 In this blog, we will learn about circular linked list traversal. Data structures are one of the most important and enjoyable to learn all concepts of computer science. Data structures are used to organize, manage, manipulate, and store data that allows modification. Let’s understand what a circular linked list is.

A linked list is said to be a circular linked list in which the last node points to the first node of the linked list which forms a circle or we can say it forms a ring, also there is no NULL in the circular linked list.

### Problem Statement of circular linked list traversal

In this question, we are given a circular linked list. We have to traverse the list and print its elements.

### Problem Statement Understanding of circular linked list traversal

Let’s try to understand the problem with the help of an example.

Suppose the given circular linked list is: Now, we have to traverse this list. So, the output after traversing the list and printing every element will be: 1 7 18 15

Input : Output: 1 7 18 15

Explanation: We will traverse the given list and while traversing we will print its elements which is the output.

This question is not a very complex one.

Let us first think about how we will traverse the circular linked list.

• As we already know, there is no NULL node in a circular linked list and hence, no endpoint. The last node of the list points back to the first node of the list.
• So, how can we do a successful traversal of the circular linked list?
We can counter this by considering any node as the starting point. This is possible because, after a complete traversal, we will reach the starting node again. So, we can use any node as the starting point.

Let us have a glance at the approach.

### Approach of traversal in circular linked list

Which node should we choose as our starting node?

• The head node will make our life easier as we already have a pointer to the head of the list.

Create a node temp and make it point to the head. Now, keep incrementing temp while temp is not equal to the head of the list. In every iteration, print the temp’s data.

As explained already, we are using the head as our starting node, and terminating the loop when we are reaching the head again.

### Algorithm to traversing in circular linked list

• If the head is NULL, simply return because the list is empty.
• Create a node temp and make it point to the head of the list.
• Now, with the help of a do-while loop, keep printing the temp – > data and increment the temp, while temp is not equal to the head of the list.
• As we have chosen the head as our starting point, we are terminating the loop when we are reaching the head again.

### Dry Run of circular linked list traversal ### Code Implementation of circular linked list traversal

```
#include <stdio.h>
#include<stdbool.h>
#include<stdlib.h>

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

void push(struct Node **head_ref, int data)
{
struct Node* ptr1 =
(struct Node*) malloc(sizeof(struct Node));
ptr1->data = data;
{
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;

}

{
{
do
{
printf("%d ",temp->data);
temp = temp->next;
}
}
}

int main(void)
{

printf( "Contents of Circular Linked List\n ");
}

```
```#include <bits stdc++.h="">
using namespace std;

class Node
{
public:
int data;
Node *next;
};

{
Node *ptr1 = new Node();
ptr1->data = data;

{
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;

}

{
{
do
{
cout << temp->data << " ";
temp = temp->next;
}
}
}

int main()
{

cout << "Contents of Circular Linked List\n ";

return 0;
}

```
```public class PrepBytes
{
static class Node
{
int data;
Node next;
};

int data)
{
Node ptr1 = new Node();
ptr1.data = data;

{
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;

}

{
{
do
{
System.out.print(temp.data + " ");
temp = temp.next;
}
}
}

public static void main(String args[])
{

System.out.println("Contents of Circular " +
}
}
```
```class Node:
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

def push(self, data):
ptr1 = Node(data)

temp = temp.next
temp.next = ptr1

else:
ptr1.next = ptr1

def printList(self):
while(True):
print (temp.data, end=" ")
temp = temp.next
break

cllist.push(15)
cllist.push(18)
cllist.push(7)
cllist.push(1)

print ("Contents of circular Linked List")
cllist.printList()

```

### Output

1 7 18 15

Time Complexity of circular linked list traversal : O(n), since we are traversing the linked list once.

## FAQs related to circular linked list traversal

1. What is traversing in circular linked list?

2. In a singly linked list, we can traverse the list from the head node and stops the traversal when we reach the Null pointer. In a circular linked list, we stop the traversal when we reach the first node again.

3. Is it faster to traverse in circular linked list?

4. Yes, it is faster to traverse the circular linked list.

5. What is a circular linked list?

6. A circular linked list is a linked list in which all nodes are connected in a circle just like a ring, there is no NULL pointer in a circular linked list.

7. Can we traverse backward in circular linked list?

8. As we know, a circular linked list forms a circle so we can traverse the circular linked list in any direction i.e. in the forward direction and backward direction.