Detect Cycle in Linked List

Linked List
Easy

Approach

Diagrams

Analysis

Time

O(n)

Space

O(1)
class Solution:
    def hasCycle(self, head):
        if not head:
            return False

        slow = head
        fast = head.next

        while slow is not fast:
            if not (fast and fast.next):
                return False

            slow = slow.next
            fast = fast.next.next

        return True

Learn

#Floyd's Cycle-Finding Algorithm

Videos