
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a linked list is Circular Linked List in C++
Here we will see, hoe to check a linked list is circular linked list or not. To check whether the linked list is circular or not, we will store the header node into some other variable, then traverse the list, if we get null at the next part of any node, then that is not circular, otherwise we will check the next node is same as the stored node or not, if so then that is circular.
Example
#include <iostream> using namespace std; class Node{ public: int data; Node *next; }; Node* getNode(int data){ Node *newNode = new Node; newNode->data = data; newNode->next = NULL; return newNode; } bool isCircularList(Node *start){ if(start == NULL) return true; Node *node = start->next; while(node != NULL && node != start){ node = node->next; } if(node == start) return true; return false; } int main() { Node *start = getNode(10); start->next = getNode(20); start->next->next = getNode(30); start->next->next->next = getNode(40); start->next->next->next->next = getNode(50); start->next->next->next->next->next = start; if (isCircularList(start)) cout << "The list is circular list"; else cout << "The list is not circular list"; }
Output
The list is circular list
- Related Articles
- Convert singly linked list into circular linked list in C++
- Check if a Linked List is Pairwise Sorted in C++
- Count nodes in Circular linked list in C++
- Python Circular Linked List Program
- Insert into a Sorted Circular Linked List in C++
- Implement circular linked list in Golang
- C++ Program to Implement Circular Singly Linked List
- C++ Program to Implement Circular Doubly Linked List
- Singly Linked List as Circular in Javascript
- Doubly Linked List as Circular in Javascript
- Convert singly linked list into XOR linked list in C++
- Convert an Array to a Circular Doubly Linked List in C++
- Sum of the nodes of a Circular Linked List in C++
- Golang program to reverse a circular linked list
- Check if a doubly linked list of characters is palindrome or not in C++

Advertisements