链表题
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
快慢指针
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *first = head;
ListNode *second = head;
while( second!= NULL && second->next != NULL)
{
first = first->next;
second = second->next->next;
if(first == second)
return true;
}
return false;
}
};