Clean C solution


  • 0
    A
    bool hasCycle(struct ListNode *head) {
        
        if(head == NULL || head->next == NULL){
            return false;
        }else{
            struct ListNode* slow = head;
            struct ListNode* fast = head;
            while(fast != NULL && fast->next != NULL ){
                slow = slow->next;
                fast = fast->next->next;
                if(slow == fast)
                    return true;
            }
        }
        return false;
    }

Log in to reply
 

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