力扣 141. 环形链表

前置

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
def hasCycle(head):
# 异步
if not head or not head.next: return False
s2, s1 = head.next, head
while s1 != s2:
if not s2 or not s2.next: return False
s1 = s1.next
s2 = s2.next.next
return True
  • 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:

请我喝杯咖啡吧~

支付宝
微信