defsorted_linked_list(p_head1, p_head2): if p_head1 isNone: return p_head2 if p_head2 isNone: return p_head1
p_node = None while p_head1 and p_head2: if p_head1 > p_head2: head = p_head2 p_head2 = p_head2.next else: head = p_head1 p_head1 = p_head1.next if p_node isNone: p_node = head head = head.next if p_head1: head.next = p_head1 else: head.next = p_head2 return p_node
deffind_min_node(lists): ifnot lists: returnNone _min = None index = 0 for i, node inenumerate(lists): if _minisNone: _min = node elif _min > node: _min = node index = i if _min.next: lists[index] = _min.next else: del lists[index] return _min
defmerge_lists(lists): head = find_min_node(lists) p_node = head while lists: p_node.next = find_min_node(lists) p_node = p_node.next return head