# Definition for singly-linked list. # class ListNode: # def init(self, val=0, next=None): # self.val = val # self.next = next
class Solution: def mergeKLists(self, lists): """ :type lists: List[ListNode] :rtype: ListNode """ res = [] for x in lists: while x: res.append(x.val) x=x.next if not res: return # sort the result list res = sorted(res) # push all the values from the list to the final linked list head = ListNode(res[0]) node = head for x in res[1:]: node.next = ListNode(x) node = node.next return head