def merge_sorted_linked_lists(head1, head2):
    if head1 is None:
        return head1
    if head2 is None:
        return head2

    head_pointer = None
    if head1.value <= head2.value:
        head_pointer = head1
        head1 = head1.next
    else:
        head_pointer = head2
        head2 = head2.next

    tail_pointer = head_pointer
    while head1 and head2:
        tmp = None

        if head1.value <= head2.value:
            tmp = head1
            head1 = head1.next
        else:
            tmp = head2
            head2 = head2.next

        # pointing tail pointer next to the smaller value
        tail_pointer.next = tmp

        # assinging tail pointer to the smaller value
        tail_pointer = tmp

    if head1:
        # list 1 not exhausted so pointing the tail (whose value will
        # be tmp of the last iteration in while loop) towards the remaining
        # part of list 1
        tail_pointer.next = head1
    elif head2:
        tail_pointer.next = head2
def merge_sorted_linked_lists(head1, head2):
    if head1 is None:
        return head1
    if head2 is None:
        return head2

    head_pointer = None
    if head1.value <= head2.value:
        head_pointer = head1
        head1 = head1.next
    else:
        head_pointer = head2
        head2 = head2.next

    tail_pointer = head_pointer
    while head1 and head2:
        tmp = None

        if head1.value <= head2.value:
            tmp = head1
            head1 = head1.next
        else:
            tmp = head2
            head2 = head2.next

        # pointing tail pointer next to the smaller value
        tail_pointer.next = tmp

        # assinging tail pointer to the smaller value
        tail_pointer = tmp

    if head1:
        # list 1 not exhausted so pointing the tail (whose value will
        # be tmp of the last iteration in while loop) towards the remaining
        # part of list 1
        tail_pointer.next = head1
    elif head2:
        tail_pointer.next = head2
# pointing tail pointer next to the smaller value # assinging tail pointer to the smaller value # list 1 not exhausted so pointing the tail (whose value will # be tmp of the last iteration in while loop) towards the remaining # part of list 1