One Pass Solution


  • 1
    L

    This will destroy the liked list but is extremely fast.

    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            while(head){
                if(head == head->next){
                    return true;
                }
                auto t = head->next;
                head->next = head;
                head = t;
            }
            return false;
        }
    };
    

Log in to reply
 

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