首页 / 客观题库

Q80541 - 线性表19

基础数据结构

题目( 单选题 )

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

 

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;

}

 

A

slow = slow->next;

fast = fast->next->next;

B

slow = fast->next;

fast = slow->next->next;

C

slow = fast->next;

fast = slow->next->next;

D

slow = fast->next;

fast = fast->next->next;

意见反馈

    最多上传3张图片,格式为JPG、PNG、JPEG,单张不超过5MB

    注册

    发送验证码

    密码必须包含数字、字母和特殊字符

    找回密码

    发送验证码

    密码必须包含数字、字母和特殊字符

    运行 ID:67149

    • 测试点1:Accepted
    • 用时:0 ms
    • 内存:288 kb
    • 测试点2:Accepted
    • 用时:0 ms
    • 内存:288 kb
    输入
    203
    输出
    203

    test

    测评信息

    错误.in文件下载

    错误.out文件下载

    运行 ID:67149

    2019-01-24 15:06:36