LeetCode解题心得,欢迎交流! 第三日
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
if(head==NULL) return false;
struct ListNode *slow=head;
struct ListNode *fast=head;
while(fast->next!=NULL && fast->next->next!= NULL)
{
slow=slow->next;
fast=fast->next->next;
if(fast==slow) return true;
}
return false;
}