Time limit excedded


  • 0
    S

    I can't understand why my code got an error saying "time limit excedded"

    struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode* mergedList = NULL;
    struct ListNode* curr_mergedList = NULL;
    struct ListNode* curr_l1 = l1;
    struct ListNode* curr_l2 = l2;
    struct ListNode* nextList = NULL;
    bool startListNode = false;

    while (curr_l1 || curr_l2) {
        nextList = malloc(sizeof(struct ListNode));
        if (!curr_l1 ? true:(!curr_l2 ? false:(curr_l1->val >= curr_l2->val))){
            nextList->val = curr_l2->val;
            curr_l2 = curr_l2->next;
        } else if (!curr_l2 ? true:(curr_l1->val <= curr_l2->val)){
            nextList->val = curr_l1->val;
            curr_l1 = curr_l1->next;
        } else {
            printf("Error\n");
        }
    
        if (!startListNode) {
            curr_mergedList = nextList;
            mergedList = nextList; //head
            startListNode = true;
        } else {
            curr_mergedList->next = nextList;
            curr_mergedList = nextList;
        }
    }
    
    return mergedList;
    

    }


Log in to reply
 

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