题解 | #合并k个已排序的链表#
合并k个已排序的链表
https://www.nowcoder.com/practice/65cfde9e5b9b4cf2b6bafa5f3ef33fa6
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param lists ListNode类一维数组 # @return ListNode类 # class Solution: def mergeKLists(self , lists: List[ListNode]) -> ListNode: # write code here newl = ListNode(0) newthis = newl liststhis = [] for listnode in lists: this = listnode liststhis.append(this) while True: minc = 999999 mini =0 minthis = None allNone = True for i in range(len(lists)): listnode = lists[i] if listnode is not None: allNone = False else: continue if listnode.val < minc: minc = listnode.val minthis = listnode mini = i if allNone: break else: lists[mini] = lists[mini].next newthis.next = minthis newthis = newthis.next return newl.next