Skip to content

Commit 93b90fc

Browse files
authored
Check If Circular Linked List.cpp
1 parent 34f94d8 commit 93b90fc

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include<iostream>
4+
using namespace std;
5+
6+
/* Link list Node */
7+
struct Node
8+
{
9+
int data;
10+
struct Node* next;
11+
12+
Node(int x){
13+
data = x;
14+
next = NULL;
15+
}
16+
17+
};
18+
19+
20+
/* Function to get the middle of the linked list*/
21+
bool isCircular(struct Node *head);
22+
23+
/* Driver program to test above function*/
24+
int main()
25+
{
26+
int T,i,n,l,k;
27+
28+
cin>>T;
29+
30+
while(T--){
31+
32+
cin>>n>>k;
33+
Node *head=NULL, *tail = NULL;
34+
int x;
35+
cin >> x;
36+
head = new Node(x);
37+
tail = head;
38+
for(int i=0;i<n-1;i++)
39+
{
40+
cin>>x;
41+
tail -> next = new Node(x);
42+
tail = tail -> next;
43+
}
44+
if (k==1 && n >= 1)
45+
tail->next = head;
46+
47+
48+
printf("%d\n", isCircular(head));
49+
}
50+
return 0;
51+
}
52+
53+
// } Driver Code Ends
54+
55+
56+
/* Link list Node
57+
struct Node
58+
{
59+
int data;
60+
struct Node* next;
61+
62+
Node(int x){
63+
data = x;
64+
next = NULL;
65+
}
66+
67+
};
68+
*/
69+
70+
/* Should return true if linked list is circular, else false */
71+
bool isCircular(Node *head)
72+
{
73+
// Your code here
74+
Node *cur=head;
75+
while(cur!=NULL)
76+
{
77+
if(cur->next==head)
78+
return 1;
79+
cur=cur->next;
80+
}
81+
return 0;
82+
}

0 commit comments

Comments
 (0)