解题思路
小学四年级追及问题
代码
class Solution {
public:
ListNode *entryNodeOfLoop(ListNode *h) {
if (!h || !h->next) return nullptr;
auto p = h->next->next, q = h->next;
while (p->next->next && p != q) p = p->next->next, q = q->next;
if (!p->next->next) return nullptr;
p = h;
while (p != q) p = p->next, q = q->next;
return p;
}
};