Python实现Floyd单链表判环算法的代码中,横线处应填写的内容是?

hs956908 单选题 共 个子题

使用Floyd算法判断单链表中是否存在环(链表头节点为head)的逻辑为:用两个指针在链表上前进,slow每次走1步,fast每次走2步,若存在环,fast终会追上slow(相遇);若无环,fast会先到达None。请补全以下代码横线处的内容:

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

def hasCycle(head: ListNode) -> bool:
    if not head or not head.next:
        return False
    slow = head
    fast = head.next
    while fast and fast.next:
        if slow == fast:
            return True
        ______________
    return False
A
slow = slow.next
fast = fast.next.next
B
slow.next = slow
fast = fast.next.next
C
slow = slow.next
fast.next = fast.next.next
D
slow = slow.next
fast = fast.next
正在加载题目数据…
已答 /
子题
该题型需在试卷/作业中作答,单题练习暂不支持
答案解析
讨论
发布
登录后参与讨论 立即登录
发布讨论需先绑定并验证手机号 去绑定手机号
加载中…
我
我
回复 @
回复 @
发送
还没有人讨论,来发表第一条吧