链表环判断
bool IsLoop(Node* node) {
node = node->next;
Node* fast = node;
Node* slow = node;
while(fast) {
fast = fast->next;
if (!fast) return false;
fast = fast->next;
if (!fast) return false;
// fast 成功走了两步
if (fast == slow) return true;
slow = slow->next;
}
}
快慢指针 fast走两步 slow走一步
由于 slow 走完一圈的时候 fast可以走两圈 所以会收敛