class Solution {
public:
ListNode *detectCycle(ListNode *head){
if(head == NULL || head->next == NULL) return NULL;
ListNode* first = head;
ListNode* second = head;
while(first != NULL && first->next != NULL){
first = first->next->next;
second = second->next;
if(first == second) break;
if(first == NULL || first->next == NULL) return NULL;
}
second = head;
while(second != first){
first = first->next;
second = second->next;
}
return second;
}
};