141. Linked List Cycle

在 Q202 ι‡Œη”¨θΏ‡ηš„ιΎŸε…”θ΅›θ·‘εΏ«ζ…’ζŒ‡ι’ˆοΌŒη›Έι‡ε³θ―ζ˜Žζœ‰ηŽ―γ€‚

202. Happy Number
class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        if not head: return False
        slow = head
        fast = head.next
        
        while fast != slow:
            if not fast or not fast.next: return False
            else:
                fast = fast.next.next
                slow = slow.next
                
        return True

Last updated

Was this helpful?