My simple 12ms c++ solution


  • 0
    L
    class Solution
    {
    public:
        bool hasCycle(ListNode * head)
        {
            if(!head)
            {
                return false;
            }
    
            ListNode * mark = new ListNode(0);
    
            ListNode * curr = head;
            ListNode * next = head->next;
            while(next)
            {
                curr->next = mark;
                if(next == mark)
                {
                    return true;
                }
                curr = next;
                next = curr->next;
            }
    
            return false;
        }
    };

Log in to reply
 

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