Leetcode腾讯精选_编号:23 --python

合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。

示例:

输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6

# 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
        """
        if not lists:
            return None
        res=[]
        for a in lists:
            while a:
                res.append(a.val)
                a=a.next
        res.sort()
        p=ListNode(0)
        q=p
        for i in range(0,len(res)):
            q.next=ListNode(res[i])
            q=q.next
        return p.next
        

猜你喜欢

转载自blog.csdn.net/qq_39959352/article/details/87970296