方法1:将链表所有元素插入数组,直接对数组排序,再返回有序列表
复杂度与快排相同
class Solution:
def mergeKLists(self , lists: List[ListNode]) -> ListNode:
# write code here
data_list = []
for head in lists:
cur = head
while cur:
data_list.append(cur.val)
cur = cur.next
data_list.sort()
N0 = ListNode(-1)
tmp = N0
for v in data_list:
tmp.next = ListNode(v)
tmp = tmp.next
return N0.next