LeetCode Merge k Sorted Lists

LeetCode解题之Merge k Sorted Lists

原题

将k个有序的链表拼接成一个有序的链表。

注意点:

  • Python中有自带的堆排序实现heapq

例子:

输入: lists = [[1->2>10],[3->9],[5->6]]
输出: 1->2->3->5->6->9->10

解题思路

整体思路与Merge Two Lists相同。不过就是从原来的两个数中取最小的节点改为从k个数中取最小的节点。这是一个典型的堆排序的应用,Python中堆排序可以用heapq实现。

AC源码

import heapq


# 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 """
        heap = []
        for node in lists:
            if node:
                heapq.heappush(heap, (node.val, node))

        temp = ListNode(-1)
        head = temp
        while heap:
            smallestNode = heapq.heappop(heap)[1]
            temp.next = smallestNode
            temp = temp.next
            if smallestNode.next:
                heapq.heappush(heap, (smallestNode.next.val, smallestNode.next))
        return head.next

欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。

你可能感兴趣的:(LeetCode,算法,python,链表)