• + 0 comments

    much shorter version:

    // handle end of list
    if(head1 == NULL){
        return head2;
    }
    if(head2 == NULL){
        return head1;
    }
    
    // handle sorting
    if(head1->data < head2->data){
        head1->next = mergeLists(head1->next, head2);
        return head1;
    }
    head2->next = mergeLists(head1, head2->next);
    return head2;