# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def detectCycle(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None
        if not head.next:
            return None
        turtle=head.next
        rabbit=head.next.next
        while turtle and rabbit:
            if turtle == rabbit:
                p=head
                while p != turtle:
                    p,turtle=p.next,turtle.next
                return p
            turtle=turtle.next
            if not rabbit.next:
                break
            rabbit=rabbit.next.next
        return None

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

相关文章:

  • 2022-01-15
  • 2021-08-11
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2021-04-07
猜你喜欢
  • 2021-10-29
  • 2021-05-18
  • 2022-02-23
  • 2022-02-15
  • 2022-02-19
  • 2021-11-14
相关资源
相似解决方案