题目描述
解题
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
p = head
q = head
flag = False
#快慢指针查找是否有环
while p != None and p.next != None:
p = p.next.next
q = q.next
if p==q:
flag = True
break
#利用公式计算第几个结点
if flag == False:
return None
else:
q = head
while q!=p:
p = p.next
q = q.next
return p