【剑指offer】Q17:合并两个排序的链表

来源:互联网 发布:android lua js 编辑:程序博客网 时间:2024/04/30 01:00
def Merge(head1, head2):if head1 == None: return head2if head2 == None: return head1psuhead = ListNode(-1)tail = psuheadwhile head1 and head2:if head1.val < head2.val: cur = head1head1 = head1.nextelse:cur = head2head2 = head2.nextcur.next = tail.nexttail.next = curtail = curPrint(psuhead)# link the rest nodesif head1 == None: head1 = head2tail.next = head1head1 = psuhead.nextdel psuheadpsuhead = Nonereturn head1

0 0