/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head)
{
if(head==NULL)
return NULL;
ListNode* slow=head;
ListNode* fast=head;
while(fast->next&&fast->next->next)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
slow=head;
while(slow!=fast)
{
slow=slow->next;
fast=fast->next;
}
return slow;
}
}
return NULL;
}
};
Linked List Cycle II
最新推荐文章于 2025-05-07 22:15:24 发布