首页 > 编程语言 > 详细

leetcode——23. 合并K个排序链表

时间:2019-10-31 21:14:33      阅读:62      评论: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
        """
        if not lists:
            return 
        #暴力法
        r=[]
        for i in range(len(lists)):
            if not lists[i]:
                continue
            head=lists[i]
            p=head
            while p:
                r.append(p.val)
                p=p.next
        if not r:
            return
        r.sort()
        head=ListNode(r[0])
        p=head
        for j in range(1,len(r)):
            p.next=ListNode(r[j])
            p=p.next
        return head
执行用时 :76 ms, 在所有 python 提交中击败了99.27%的用户
内存消耗 :20 MB, 在所有 python 提交中击败了18.16%的用户
 
——2019.10.31

leetcode——23. 合并K个排序链表

原文:https://www.cnblogs.com/taoyuxin/p/11773559.html

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