You are viewing a single comment's thread. Return to all comments →
Iterative solution:
def addToList(data, theList): node = SinglyLinkedListNode(data) if theList is not None: theList.next = node return node def mergeLists(head1, head2): theList = None top = None while head1 is not None or head2 is not None: if head2 is None: theList = addToList(head1.data, theList) head1 = head1.next elif head1 is None: theList = addToList(head2.data, theList) head2 = head2.next elif head1.data < head2.data: theList = addToList(head1.data, theList) head1 = head1.next else: theList = addToList(head2.data, theList) head2 = head2.next if top is None: top = theList return top
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 →
Iterative solution: