LeetCode 23 Merge k Sorted Lists (Python)

Posted by 小明MaxMing on January 24, 2021

题目

You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.

Merge all the linked-lists into one sorted linked-list and return it.

解题思路

把每个链表的第一个元素已经链表的标号放到优先队列里,每次pop最小的,如果后面仍有元素就继续加入到队列里

代码

class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        q = []
        for i in range(len(lists)):
            if lists[i]:
                heapq.heappush(q, (lists[i].val, i))
                lists[i] = lists[i].next
        res = ListNode()
        cur = res
        while q:
            val, i = heapq.heappop(q)
            cur.next = ListNode(val)
            cur = cur.next
            if lists[i]:
                heapq.heappush(q, (lists[i].val, i))
                lists[i] = lists[i].next
        return res.next

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅