class Solution
{
public:
ListNode *detectCycle(ListNode *head)
{
ListNode *slowA = head;
ListNode *fastA = head;
while (fastA && fastA->next)
{
slowA = slowA->next;
fastA = fastA->next->next;
if (slowA == fastA)
{
ListNode *slowB = head;
while (slowA != slowB)
{
slowA = slowA->next;
slowB = slowB->next;
}
return slowB;
}
}
return nullptr;
}
};
leetcode 142. Linked List Cycle II (medium)
最新推荐文章于 2022-10-08 18:41:22 发布