-
Notifications
You must be signed in to change notification settings - Fork 178
/
circularLinkedList.cpp
49 lines (40 loc) · 1.09 KB
/
circularLinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *next;
Node(int x)
{
data = x;
next = NULL;
}
};
// This function returns true if given linked list is circular, else false.
bool isCircular(Node *head)
{
// If the head is null, the linked list is empty, so it is not circular
if (!head)
return false;
// Traverse the linked list until either the end is reached or the next node is equal to the head
Node *curr = head;
while (curr->next && curr->next != head)
curr = curr->next;
// If the end is reached before finding the head again, the linked list is not circular
if (curr->next == NULL)
return false;
// If the head is found again before reaching the end, the linked list is circular
return true;
}
int main()
{
Node *head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
isCircular(head) ? cout << "Yes\n" : cout << "No\n";
// Making linked list circular
head->next->next->next->next = head;
isCircular(head) ? cout << "Yes\n" : cout << "No\n";
return 0;
}