C++ solution sharing(12ms)


  • 3
    A
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            ListNode* fast=head;
            ListNode* slow=head;
            while(fast)
            {
                if(fast->next)
                    fast=fast->next->next;
                else
                    return false;
                slow=slow->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.