参考于:
https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/linked-list-cycle-ii-kuai-man-zhi-zhen-shuang-zhi-/
注意要用距离、几何来理解这道题
使用快慢指针
# 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:
if head == None:
return None
slow = head
fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
if not fast.next or not fast.next.next:
return None
count2 = 0
fast = head
while fast != slow:
fast = fast.next
slow = slow.next
count2 += 1
# 求环的大小
# f = fast
# s = slow
# c = 0
# while f.next and f.next.next:
# f = f.next.next
# s = s.next
# c += 1
# if s == f:
# print(c)
# break
return fast
简单的写法:
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
if not head:
return None
fast = head
slow = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
break
if not fast.next or not fast.next.next:
return None
fast = head
while fast != slow:
fast = fast.next
slow = slow.next
return slow
把关键的点,记录一下:
快指走过的路程是环形周长的整数倍,F= N*R
这点比较关键
另外,但慢指针走过的路程为(N*R+K)
时,指针的位置一定处于环形的入口处
关于环中相遇的位置:
但慢指针刚好到达环的入口时,s距离f的距离为
δ
x
\delta x
δx ,那么相遇的位置会是对称的y位置,解释如下:当他们相遇时,f要比s多走
R
−
δ
x
R-\delta x
R−δx, 设时间为t,
2
t
−
t
=
R
−
δ
x
2t-t =R-\delta x
2t−t=R−δx,
t
=
R
−
δ
x
t = R-\delta x
t=R−δx, 因此相遇的位置是:对于s
0
+
R
−
δ
x
0+R-\delta x
0+R−δx 即 对称的位置
y
y
y。