# 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:
            return False
        if not head.next:
            return False
        turtle=head.next
        rabbit=head.next.next
        while turtle and rabbit:
            if turtle == rabbit:
                return True
            turtle=turtle.next
            if not rabbit.next:
                return False
            rabbit=rabbit.next.next
        return False

@https://github.com/Linzertorte/LeetCode-in-Python/blob/master/LinkedListCycle.py

相关文章:

  • 2021-10-23
  • 2021-11-13
  • 2021-07-16
  • 2021-11-22
  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-03-08
  • 2021-05-19
  • 2021-10-29
  • 2021-09-27
  • 2021-12-28
  • 2022-01-31
  • 2021-11-14
相关资源
相似解决方案