Merge two sorted linked lists

  • + 0 comments

    Does anyone understand why this code fails some tests? Solution by recursion, identical to the Editorial.. Conditions and indentation for clarity

    def mergeLists(head1, head2):
        if head1 is None and head2 is None:
            return None
        elif head1 is None:
            return head2
        elif head2 is None:
            return head1
        else:
            if head1.data < head2.data:
                head1.next = mergeLists(head1.next, head2)
                return head1
            else:
                head2.next = mergeLists(head1, head2.next)
                return head2