23. 合并K个排序链表
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
示例:
输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6
暴力求解:
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
ans = []
for i in range(len(lists)):
while lists[i]:
ans.append(lists[i].val)
lists[i] = lists[i].next
ans = sorted(ans)
res = ListNode(None)
follow = res
for i in ans:
t = ListNode(i)
follow.next = t
follow = t
return res.next