Funny and possibly shortest running solution. One cycle!


  • 0
    P
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            while(head != NULL)
            {
                if((head -> next) == (ListNode*)0xa) return true;
                auto k = head -> next;
                head -> next = (ListNode*)0xa;
                head = k;
            }
            return false;
    
        }
    };

Log in to reply
 

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