# Find the middle of a linked list with two pointers.
# Time: O(n), Space: O(1)
def middleOfList(head):
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
# Determine if the linked list contains a cycle.
# Time: O(n), Space: O(1)
def hasCycle(head):
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
# Determine if the linked list contains a cycle and
# return the beginning of the cycle, otherwise return null.
# Time: O(n), Space: O(1)
def cycleStart(head):
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
if not fast or not fast.next:
return None
slow2 = head
while slow != slow2:
slow = slow.next
slow2 = slow2.next
return slow