8 lines concise and easy understand c++ solution


  • 4
    A
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            if(!head) return false;
            ListNode* fast(head), * slow(head);
            while(fast->next && fast->next->next){
                slow = slow->next;
                fast = fast->next->next;
                if(fast == slow) return true;
            }
            return false;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.