# 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