力扣 142. 环形链表II

前置

1
2
3
4
5
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

解法

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def detectCycle(head):
# 异步
if not head or not head.next:
return None
s1, s2 = head, head
while s2 and s2.next:
s1 = s1.next
s2 = s2.next.next
if s1 == s2: break
if not s2 or not s2.next: return
s2 = head
while s1 != s2:
s1 = s1.next
s2 = s2.next

return s2
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信