141. Linked List Cycle

https://leetcode.com/problems/linked-list-cycle/

solution

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        if head is None or head.next is None:
            return False

        slow = head
        fast = head.next
        while fast is not None and fast.next is not None:
            if slow == fast:
                return True

            slow = slow.next
            fast = fast.next.next

        return False

时间复杂度:O(n) 空间复杂度:O(1)

follow up

  • SPFA 判断是否有负环

Last updated