Skip to content

Commit 81c4ef6

Browse files
committed
Sync LeetCode submission - Linked List Cycle (c)
1 parent 5c886ba commit 81c4ef6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* struct ListNode *next;
6+
* };
7+
*/
8+
#include <stdbool.h>
9+
10+
bool hasCycle(struct ListNode *head) {
11+
if (!head) { return false; };
12+
struct ListNode* fast = head;
13+
struct ListNode* slow = head;
14+
15+
while (true) {
16+
fast = fast->next;
17+
if (!fast) { return false; };
18+
if (fast == slow) { return true; };
19+
fast = fast->next;
20+
if (!fast) { return false; };
21+
if (fast == slow) { return true; };
22+
slow = slow->next;
23+
}
24+
25+
26+
27+
}

0 commit comments

Comments
 (0)