Skip to content

Has Cycle

LeetCode Problem

# https://leetcode.com/problems/linked-list-cycle
# Standard slow fast problem
class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        slow, fast = head, head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if fast == slow:
                return True
        return False