题目链接 | 141. 环形链表 |
---|---|
思路 | 快慢指针-经典问题 |
题解链接 | 没想明白?一个视频讲透快慢指针!(Python/Java/C++/Go/JS) |
关键点 | while fast and fast.next: ... |
时间复杂度 | \(O(n)\) |
空间复杂度 | \(O(1)\) |
代码实现:
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if fast is slow:
return True
return False
标签:slow,141,环形,fast,next,链表
From: https://www.cnblogs.com/WrRan/p/18409120