zoukankan      html  css  js  c++  java
  • [leetcode]23. Merge k Sorted Lists

     意外的简单,也可能是无耻的用了sort的缘故。

    Runtime: 68 ms, faster than 96.70% of Python3 online submissions forMerge k Sorted Lists.
    Memory Usage: 17.3 MB, less than 19.39% of Python3 online submissions for Merge k Sorted Lists.
     

    Submission Detail

    131 / 131 test cases passed.
    Status: 

    Accepted

    Runtime: 68 ms
    Memory Usage: 17.3 MB
    Submitted: 1 minute ago
     
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def mergeKLists(self, lists: List[ListNode]) -> ListNode:
            lengthS = len(lists)
            #0
            if lengthS ==0:
                return []
            val =[]
            for index in range(lengthS):
                if lists[index] ==None:
                    continue
                temp = lists[index]
                while(temp != None):
                    val.append(temp.val)
                    temp = temp.next
            if len(val)==0:
                return []
            #sort
            val.sort()
            #ret
            temp = ListNode(val[0])
            ret = temp
            for index in range(1,len(val)):
                temp.next = ListNode(val[index])
                temp = temp.next
                
            return ret
     
  • 相关阅读:
    web测试学习大纲
    Python语言编程基础
    python文件IO
    Python 正则表达式
    python官网导航翻译
    python常用库
    python连接数据库
    sublime与python交互
    selenium连接浏览器方式
    sublime中运行python时编码格式问题
  • 原文地址:https://www.cnblogs.com/alfredsun/p/10944149.html
Copyright © 2011-2022 走看看