Merge two sorted linked lists

  • + 4 comments

    python solution:

    def mergeLists(head1, head2):
        if not head1 and not head2:
            return head1
        if not head1:
            return head2
        if not head2:
            return head1
        res = SinglyLinkedListNode(None)
        c = res
        c1 = head1
        c2 = head2
        while c1 and c2:
            print(res)
            if c1.data <= c2.data:
                c.next = c1
                c1 = c1.next
            else:
                c.next = c2
                c2 = c2.next
            c = c.next
        if c1:
            c.next = c1
        if c2:
            c.next = c2
        return res.next