问题描述
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
思路
def detectCycle(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
slow , fast = head , head
meet = None
loop = False
while fast and fast.next and not loop:
slow,fast = slow.next,fast.next.next
if slow is fast:
loop = True
if loop:
start = head
while start != slow:
start = start.next
slow = slow.next
return slow
return None