why my quick sort time limit exceeded?


  • 0
    C

    Can anyone tell me why my program got time limit exceeded?
    Thanks!

    void quicksort(struct ListNode* privot, int length) {
        struct ListNode *current_node = privot;
        struct ListNode *sorted_tail = privot;
        int left_length = 0;
        int temp;
        int standard;
        
        if (0 < length) {
            standard = privot->next->val;
            while (NULL != current_node->next) {;
                current_node = current_node->next;
                if (current_node->val < standard) {
                    left_length += 1;
                    temp = sorted_tail->next->val;
                    sorted_tail->next->val = current_node->val;
                    current_node->val = sorted_tail->next->next->val;
                    sorted_tail->next->next->val = temp;
                    sorted_tail = sorted_tail->next;
                }
            }
            quicksort(privot, left_length);
            quicksort(sorted_tail->next, length - left_length - 1);
        }
    }
    
    struct ListNode* sortList(struct ListNode* head) {
        struct ListNode privot;
        int length = 0;
        struct ListNode *current_node = head;
        privot.next = head;
        while (NULL != current_node) {
            length += 1;
            current_node = current_node->next;
        }
        quicksort(&privot, length);
        return privot.next;
    }
    

Log in to reply
 

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