mycode  98.22%

# 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
        """
        if not head or not head.next:
            return False
        fast = slow = head
        while fast.next and fast.next.next:
            slow = slow.next
            fast = fast.next.next
            if  slow == fast:
                return True
        return False

 

参考

可以稍微简化一丢丢

# 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
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False

 

相关文章:

  • 2022-02-02
  • 2022-01-05
  • 2021-10-04
  • 2021-07-08
  • 2021-08-08
  • 2021-09-22
  • 2022-01-06
  • 2022-03-08
猜你喜欢
  • 2021-12-16
  • 2021-06-05
  • 2022-01-09
  • 2021-04-23
  • 2021-09-02
  • 2021-09-14
  • 2021-12-05
相关资源
相似解决方案