Skip to content

Latest commit

 

History

History
28 lines (21 loc) · 696 Bytes

File metadata and controls

28 lines (21 loc) · 696 Bytes

141. Linked List Cycle, Easy

  • a Must know problem
  • Idea is to use two pointer, one is slow and one is fast.
  • Traverse them, one with normal rate, other one with twice the rate,
  • If they match somewhere then there must be cycle, else not.
Code
   bool hasCycle(ListNode *head) {
        if (!head) return false;
        auto s = head; 
        auto f = head;
        
        while (f -> next and f -> next -> next) {
            s = s -> next;
            f = f -> next -> next;
            
            if (s == f) return true;
        }
        return false;
        
    }