/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*///一次AC才算优秀,可我WA了三次
class Solution {
public:
bool hasCycle(ListNode *head)
{
if(head==NULL)
return false;
ListNode* fast=head->next;
ListNode* slow=head;
while(fast&&slow&&fast!=slow)
{
if(fast->next!=NULL)
fast=fast->next;
fast=fast->next;
slow=slow->next;
}
if(fast&&slow&&fast==slow)
return true;
return false;
}
};
LeetCode-Linked List Cycle
最新推荐文章于 2014-09-22 20:13:31 发布