Merge two sorted linked lists

  • + 0 comments
    sys.setrecursionlimit(2000)
    
    def mergeLists(head1, head2):
        if not (head1 and head2):
            return head1 if head1 else head2
    
        if head1.data < head2.data:
            head1.next = mergeLists(head1.next, head2)
            return head1
        else:
            head2.next = mergeLists(head1, head2.next)
            return head2