You are viewing a single comment's thread. Return to all comments →
Good solution, but it break with for larger test cases because of "Maximum recursion depth exceeded" error:
Here is a less elegant non-recursive one:
def mergeLists(head1, head2): if head1 is None: return head2 if head2 is None: return head1 if head1.data <= head2.data: head = head1 curr1 = head1.next curr2 = head2 else: head = head2 curr1 = head1 curr2 = head2.next curr = head while True: if curr1 is None: curr.next = curr2 break elif curr2 is None: curr.next = curr1 break if curr1.data <= curr2.data: curr.next = curr1 curr1 = curr1.next else: curr.next = curr2 curr2 = curr2.next curr = curr.next return head
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 →
Good solution, but it break with for larger test cases because of "Maximum recursion depth exceeded" error:
Here is a less elegant non-recursive one: