Easy #025

141. Linked List Cycle

给定链表头节点 head,判断链表中是否有环。若有环返回 true,否则返回 false。

初始化
慢指针步进
快指针步进
判定
点击「加载」或切换示例开始可视化
0 / 0

📋 步骤详情

—

🐍 Python 代码

class Solution:
    def hasCycle(self, head):
        if not head or not head.next:
            return False
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False