一、题目
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
示例1
输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6
二、代码
python
def mergeKLists(lists):
head = ListNode(None)
cur = head
# 一次遍历取到所有值
nodes = []
for listnode in lists:
while listnode:
nodes.append(listnode.val)
listnode = listnode.next
# 排序
nodes = sorted(nodes)
# 创建ListNode
for val in nodes:
cur.next = ListNode(val)
cur = cur.next
return head.next