Skip to main content

2 - Linked List Cycle

Problem Statement

Approch 1: Fast and slow pointers approch

Written by @kondekarshubham123
from linked_list import LinkedList

def detect_cycle(head):
if head == None or head.next == None:
return False

slow_pointer = head
fast_pointer = head.next

while fast_pointer.next and fast_pointer.next.next and slow_pointer != fast_pointer:
slow_pointer = slow_pointer.next
fast_pointer = fast_pointer.next.next

return slow_pointer == fast_pointer