题目如下:
解题思路:用最小堆,优先级队列都可以。我尝试了先把所有node的val都取出来,然后排序,最后组成新的链表,没想到也能被AC。
代码如下:
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ vl = [] for i in lists: while i != None: vl.append(i.val) i = i.next vl.sort() head = None current = None for i in vl: ln = ListNode(i) if head == None: head = ln current = head else: current.next = ln current = current.next return head