标签:== list fast code ptr 返回 save ret ext
ListNode *detectCycle(ListNode *head) {
set<ListNode *> save;
while (head != nullptr)
if (save.find(head) != save.end())
return *save.find(head);
else {
save.emplace(head);
head = head->next;
}
return nullptr;
}
ListNode *detectCycle(ListNode *head) {
if(head==nullptr)
return nullptr;
ListNode *slow = head, *fast = head;
while(true){
if(fast->next==nullptr||fast->next->next==nullptr)
return nullptr;
slow=slow->next;
fast=fast->next->next;
if(slow==fast){
while(head!=fast){
head=head->next;
fast=fast->next;
}
return head;
}
}
}
标签:== list fast code ptr 返回 save ret ext
原文地址:https://www.cnblogs.com/INnoVationv2/p/10152627.html