Linked List Cycle
follow up:
Can you solve it without using extra space?
bool hasCycle(ListNode *head) {
/*
加上这句就超时我也是醉了
if(NULL == head && NULL == head->next)
{
return false;
}*/
ListNode *first = head;
ListNode *second = head;
while(first!=NULL&&second!=NULL&&second->next!=NULL)
{
first = first->next;
second = second->next->next;
if(first == second)
{
return true;
break;
}
}
return false;
}