第20726题 单选题
补全Floyd快慢指针法判断单链表是否存在环的C++代码,横线处应填入哪项

函数 hasCycle 采用Floyd快慢指针法判断一个单链表中是否存在环,链表的头节点为 head,即用两个指针在链表上前进: slow 每次走 1 步, fast 每次走 2 步,若存在环, fast 终会追上 slow (相遇);若无环, fast 会先到达 nullptr,则横线上应填写( )。

struct Node {
    int val;
    Node *next;
    Node(int x) : val(x), next(nullptr) {}
};
bool hasCycle(Node *head) {
    if (!head || !head->next)
        return false;
    Node* slow = head;
    Node* fast = head->next;
    while (fast && fast->next) {
        if (slow == fast) return true;
        _______________________ // 在此填入代码
    }
    return false;
}
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析