题目信息

解题思路

代码

  # Definition for singly-linked list.
  # class ListNode:
  #     def __init__(self, x):
  #         self.val = x
  #         self.next = None
  
  class Solution:
	  def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
		  fast, slow = head, head
		  if head is None or fast.next is None:
			  return None
		  while fast is not None and slow is not None and fast.next is not None:
			  fast = fast.next.next
			  slow = slow.next
			  if slow == fast:
				  break
		  if slow != fast:
			  return None
		  else:
			  fast = head
			  while slow != fast:
				  slow = slow.next
				  fast = fast.next
			  return slow