Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def detectCycle(self, head):
- if not head or not head.next:
- return None
- slow = fast = head
- # Step 1: detect cycle
- while fast and fast.next:
- slow = slow.next
- fast = fast.next.next
- if slow == fast:
- break
- if slow!=fast:
- return None # no cycle
- # Step 2: find cycle start
- slow = head
- while slow != fast:
- slow = slow.next
- fast = fast.next
- return slow
Advertisement