看到题解贼搞笑一句话
- 史上最菜转圈圈
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
slow = fast = head
if head:
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
- 快慢指针
- 一个快一个慢
- 如果相遇,说明有圈圈