Why my code TLE in case of {3,2,4}


  • 0
    M
     if (!head || !head->next) {
            return head;
        }
        ListNode *shead = head;
        head = head->next;
        shead->next = NULL;
        
        for (ListNode *p1 = head; p1; ) {
            ListNode *p3 = p1->next;
            int key = p1->val;
            for (ListNode *p2 = shead; p2; p2->next) {
                if (key > p2->val) {
                    if (p2->next && key > p2->next->val) {
                        continue;
                    } else {
                        p1->next = p2->next;
                        p2->next = p1;
                        break;
                    }
                } else {
                    p1->next = p2;
                    shead = p1;
                    break;
                }
            }
            p1 = p3;
        }
        return shead;

  • 0
    M

    I've check this codes on my pc and it work fine.


Log in to reply
 

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