-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102.h
More file actions
32 lines (30 loc) · 735 Bytes
/
102.h
File metadata and controls
32 lines (30 loc) · 735 Bytes
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
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param head: The first node of linked list.
* @return: True if it has a cycle, or false
*/
bool hasCycle(ListNode * head) {
//if(head == nullptr || head->next == nullptr) return false;
// write your code here
ListNode *fast = head, *slow = head;
while(fast != nullptr && fast->next != nullptr){
fast = fast->next->next;
slow = slow->next;
if(fast == slow) return true;
}
return false;
}
};