Linked List Cycle - Implementation

Here is the implementation using Floyd's algorithm:

function hasCycle(head):
    if head == null:
        return false

    slow = head
    fast = head

    while fast != null and fast.next != null:
        slow = slow.next
        fast = fast.next.next

        if slow == fast:
            return true

    return false

This runs in O(n)O(n) time and O(1)O(1) space (excluding input). Floyd's Tortoise and Hare algorithm is the standard approach for cycle detection.