In a conventional linked list, we traverse the list from the head node and stop the traversal when we reach NULL. In a circular linked list, we stop traversal when we reach the first node again. Following is the C code for the linked list traversal.
C++
/* Function to print nodes in
a given Circular linked list */
voidprintList(Node* head)
{
Node* temp = head;
// If linked list is not empty
if(head != NULL) {
// Print nodes till we reach first node again
do{
cout << temp->data << " ";
temp = temp->next;
} while(temp != head);
}
}
C
/* Function to traverse a given Circular linked list and print nodes */
voidprintList(structNode *first)
{
structNode *temp = first;
// If linked list is not empty
if(first != NULL)
{
// Keep printing nodes till we reach the first node again
do
{
printf("%d ", temp->data);
temp = temp->next;
}
while(temp != first);
}
}
Java
/* Function to print nodes in a
given Circular linked list */
importjava.util.*;
staticvoidprintList(Node head)
{
Node temp = head;
// If linked list is not empty
if(head != null)
{
// Keep printing nodes till we reach the first node
// again
do
{
System.out.print(temp.data + " ");
temp = temp.next;
} while(temp != head);
}
}
// This code is contributed by pratham76.
Python3
# Function to print nodes in a given Circular linked list
defprintList(self):
temp =self.head
# If linked list is not empty
ifself.head isnotNone:
while(True):
# Print nodes till we reach first node again
print(temp.data, end=" ")
temp =temp.next
if(temp ==self.head):
break
C#
/* Function to print nodes in a
given Circular linked list */
staticvoidprintList(Node head)
{
Node temp = head;
// If linked list is not empty
if(head != null) {
// Keep printing nodes till we reach the first node
// again
do{
Console.Write(temp.data + " ");
temp = temp.next;
} while(temp != head);
}
}
//This code is contributed by rutvik_56
Javascript
<script>
/* Function to print nodes in a
given Circular linked list */
functionprintList(head)
{
vartemp = head;
// If linked list is not empty
if(head != null)
{
// Keep printing nodes till we reach the first node
// again
do
{
document.write(temp.data + " ");
temp = temp.next;
} while(temp != head);
}
}
// This code contributed by umadevi9616
</script>
Time Complexity: O(n) Auxiliary Space: O(1)
Following are complete programs to demonstrate the traversal of a circular linked list.
C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
usingnamespacestd;
/* structure for a node */
classNode
{
public:
intdata;
Node *next;
};
/* Function to insert a node at the beginning
of a Circular linked list */
voidpush(Node **head_ref, intdata)
{
Node *ptr1 = newNode();
Node *temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
/* If linked list is not NULL then
set the next of last node */
if(*head_ref != NULL)
{
while(temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1; /*For the first node */
*head_ref = ptr1;
}
/* Function to print nodes in
a given Circular linked list */
voidprintList(Node *head)
{
Node *temp = head;
if(head != NULL)
{
do
{
cout << temp->data << " ";
temp = temp->next;
}
while(temp != head);
}
}
/* Driver program to test above functions */
intmain()
{
/* Initialize lists as empty */
Node *head = NULL;
/* Created linked list will be 11->2->56->12 */
push(&head, 12);
push(&head, 56);
push(&head, 2);
push(&head, 11);
cout << "Contents of Circular Linked List\n ";
printList(head);
return0;
}
C
// C program to implement
// the above approach
#include<stdio.h>
#include<stdlib.h>
/* structure for a node */
structNode
{
intdata;
structNode *next;
};
/* Function to insert a node at the beginning of a Circular
We will soon be discussing the implementation of insert-delete operations for circular linked lists. Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!