141. Linked List Cycle

Given head, the head of a linked list, determine if the linked list has a cycle in it.

Return true if there is a cycle in the linked list. Otherwise, return false.

 

Example:



def hasCycle(head: Optional[ListNode]):
       
    slow = head;
    fast = head;
   
    while fast and fast.next:
       
        slow = slow.next
        fast = fast.next.next
       
        if slow == fast:
            return True
   
    return False

Comments

Popular posts from this blog

849. Maximize Distance to Closest Person

347. Top K Frequent Elements

139. Word Break