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 time and space (excluding input). Floyd's Tortoise and Hare algorithm is the standard approach for cycle detection.