C clean and simple, 3ms (99%)


  • 0
    J
    struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
        struct ListNode* node = (struct ListNode* ) malloc(sizeof(struct ListNode));
        struct ListNode* root=node;
        while(l1!=NULL || l2!=NULL){
            if(l1==NULL){
                node->next=l2;
                return root->next;
            }else if(l2==NULL){
                node->next = l1;
                return root->next;
            }else if(l1->val <= l2->val){
                node->next = l1;
                l1=l1->next;
            }else{
                node->next=l2;
                l2=l2->next;
            }
            node=node->next;
        }
        return root->next;
    }
    

Log in to reply
 

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