class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow=head;
ListNode* fast=head;
while(fast!=NULL&&fast->next!=NULL){
slow=slow->next;
fast=fast->next->next;
if(slow==fast)break;
}
if(fast==NULL||fast->next==NULL)return NULL;
slow = head;
while(slow!=fast){
slow=slow->next;
fast=fast->next;
}
return slow;
}
};
刷题记录第二十七天-环形链表II
最新推荐文章于 2024-06-16 09:46:44 发布