python内置库heapq
class Solution(object):
def getLeastNumbers_Solution(self, input, k):
"""
:type input: list[int]
:type k: int
:rtype: list[int]
"""
import heapq
heap = []
for i in input:
heapq.heappush(heap, i)
return heapq.nsmallest(k, heap)
提供一个思路,也可以吧链表后部分删除