Given two sorted linked lists, merge them such that resulting linked list is also sorted.
class sll(object):
def __init__(self,val):
self.value = val
self.next = None
def __repr__(self):
return str(self.value)
def merge_sorted_lists(head1,head2):
result = sll(0)
current = result
while head1 and head2:
if head1.value < head2.value:
current.next = head1
head1 = head1.next
elif head2.value < head1.value:
current.next = head2
head2 = head2.next
else:
current.next = head1
head1 = head1.next
current = current.next
current.next = head2
head2 = head2.next
current = current.next
while head1:
current.next = head1
head1 = head1.next
current = current.next
while head2:
current.next = head2
head2 = head2.next
current = current.next
return result.next