• + 0 comments

    Thank you Dark_Knight19 for posting your solution! I was struggling with solving this non-recusively for hours, and your post inspired me to try a recursive solution.

    I ended up coming up with this solution:

    if(head1 == nullptr && head2 == nullptr)
    {
        return nullptr;
    }
    if(head1 == nullptr && head2 != nullptr)
    {
        return head2;
    }
    if(head1 != nullptr && head2 == nullptr)
    {
        return head1;
    }
    
    if(head1->data < head2->data)
    {
        SinglyLinkedListNode * temp = head1->next;
        head1->next = mergeLists(temp, head2);
        return head1;
    
    }
    else
    {
        SinglyLinkedListNode * temp = head2->next;
        head2->next = mergeLists(temp, head1);
        return head2;
    }