Skip to content

Commit b2580c8

Browse files
committed
check circular LL
1 parent 2a58825 commit b2580c8

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
- [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/ "view question") - [Cpp Solution](./solutions/Middle%20of%20the%20Linked%20List.cpp)
139139
- [Merge Sort for Linked List](https://practice.geeksforgeeks.org/problems/sort-a-linked-list/1# "view question") - [Cpp Solution](./solutions/Merge%20Sort%20for%20Linked%20List.cpp)
140140
- [Quick Sort on Linked List](https://practice.geeksforgeeks.org/problems/quick-sort-on-linked-list/1# "view question") - [Cpp Solution](./solutions/Quick%20Sort%20on%20Linked%20List.cpp)
141+
- [Check If Circular Linked List](https://practice.geeksforgeeks.org/problems/circular-linked-list/1# "view question") - [Cpp Solution](./solutions/Check%20If%20Circular%20Linked%20List.cpp)
141142
- []( "view question") - [Cpp Solution](./solutions/.cpp)
142143

143144
### Binary Tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Check If Circular Linked List
3+
=============================
4+
5+
Given a singly linked list, find if the linked list is circular or not. A linked list is called circular if it not NULL terminated and all nodes are connected in the form of a cycle. An empty linked list is considered as circular.
6+
7+
Example 1:
8+
Input:
9+
LinkedList: 1->2->3->4->5
10+
(the first and last node is connected,
11+
i.e. 5 --> 1)
12+
Output: 1
13+
14+
Example 2:
15+
Input:
16+
LinkedList: 2->4->6->7->5->1
17+
Output: 0
18+
Your Task:
19+
The task is to complete the function isCircular() which checks if the given linked list is circular or not. It should return true or false accordingly. (the driver code prints 1 if the returned values is true, otherwise 0)
20+
21+
Expected Time Complexity: O(N).
22+
Expected Auxiliary Space: O(1).
23+
24+
Constraints:
25+
1 <=Number of nodes<= 100
26+
*/
27+
28+
bool isCircular(Node *head)
29+
{
30+
auto temp = head;
31+
while (temp)
32+
{
33+
temp = temp->next;
34+
if (temp == head)
35+
return true;
36+
}
37+
return false;
38+
}

0 commit comments

Comments
 (0)