首页 > 代码库 > 【剑指offer】Q17:合并两个排序的链表

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

def Merge(head1, head2):
	if head1 == None: return head2
	if head2 == None: return head1
	psuhead = ListNode(-1)
	tail = psuhead
	while head1 and head2:
		if head1.val < head2.val: 
			cur = head1
			head1 = head1.next
		else:
			cur = head2
			head2 = head2.next
		cur.next = tail.next
		tail.next = cur
		tail = cur
		Print(psuhead)
	# link the rest nodes
	if head1 == None: head1 = head2
	tail.next = head1
	head1 = psuhead.next
	del psuhead
	psuhead = None
	return head1