leetcode-141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
class Solution {
public:
bool hasCycle(ListNode *head) {
// if(!head || !head->next || !head -> next -> next) return false;
ListNode* fast = head;
ListNode* slow = head;
while(fast && fast->next){
fast = fast->next->next;
slow = slow->next;
if(fast == slow)
return true;
}
return false;
}
};