LeetCode-23-Merge-k-Sorted-Lists Python倒循环

来源:互联网 发布:linux关闭nagle算法 编辑:程序博客网 时间:2024/05/16 19:08

这个题本来应该用类似归并排序的思想的,然而我直接暴力了一发就过了。。。不明觉厉

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def mergeKLists(self, lists):        """        :type lists: List[ListNode]        :rtype: ListNode        """        Len=len(lists)        for i in range(Len-1,-1,-1):            if lists[i]==None:del lists[i]        if len(lists)==0:return []                ans=ListNode(0)        h=ans;        while len(lists)>0:            m=lists[0].val            cur=0            for i in range(1,len(lists)):                if lists[i].val<=m:                    cur=i                    m=lists[i].val            ans.next=ListNode(m)            ans=ans.next            lists[cur]=lists[cur].next            if lists[cur]==None:                del lists[cur];        return h.next                                


原创粉丝点击