Sunday, September 10, 2017

141. Linked List Cycle

https://leetcode.com/problems/linked-list-cycle/description/
Solution , fast and slow runner
    bool hasCycle(ListNode *head) {
        if(!head) return false;
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast->next && fast->next->next) {
            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast) return true;
        }
        return false;
    }

No comments:

Post a Comment