[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

 

 

你可能感兴趣的:([leetcode]23. Merge k Sorted Lists)