Templates
Template: 2. TWO POINTERS — Same Direction (Fast/Slow)
Mark when done:
2. TWO POINTERS — Same Direction (Fast/Slow)
# =============================================================================
# Use when: cycle detection, find middle, remove duplicates in-place
def fast_slow_pointers(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True # cycle detected
return False # slow is now at middle (if no cycle)
# =============================================================================