算法-链的操作(一)-合并两个排序的链接(no.25)
合并两个排序的链接(no.25)
把下面连个排好序的链,从小到大排序链接。
list1 : 1 -> 6 -> 8
list2 : 2-> 5 -> 9
def merge(head1, head2): if(head1 == None): return head1 if(head2 == None): return head2 node = ListNode() if(head1.val<head2.val): node = head1 head1.next = merge(head1.next,head2) else: node = head2 head2.next = merge(head1, head2.next) return node
输出: 1 -> 2 -> 5 -> 6 -> 8 -> 9