首页 > 编程语言 > 详细

leetcode Merge K sorted Lists python

时间:2015-11-24 23:37:19      阅读:458      评论:0      收藏:0      [点我收藏+]
# 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 != None:
                heap.append((node.val,node))
        heapq.heapify(heap)
        head=ListNode(0)
        curr=head
        while heap:
            pop=heapq.heappop(heap)
            curr.next=pop[1]
            curr=curr.next
            if pop[1].next:
                heapq.heappush(heap,(pop[1].next.val,pop[1].next))
        return head.next

 

leetcode Merge K sorted Lists python

原文:http://www.cnblogs.com/allenhaozi/p/4993271.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!