We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def mergeLists(head1, head2):
# Initializing head of the result
if head1 == None:
return head2
if head2 == None:
return head2
if head1.data < head2.data:
head = head1
head1 = head1.next
else:
head = head2
head2 = head2.next
# Moving through two lists
prev = head
while True:
if head1 == None:
prev.next = head2
return head
if head2 == None:
prev.next = head1
return head
if head1.data < head2.data:
prev.next = head1
head1 = head1.next
else:
prev.next = head2
head2 = head2.next
prev = prev.next
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Merge two sorted linked lists
You are viewing a single comment's thread. Return to all comments →
I have almost the same: