# from sys import modules # 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]): # write code here res_head = ListNode(0) cur_node = res_head nums_list = [] # 数字列表 for node in lists: if node == None: # 判断链表节点非空 continue while node.next != None: # 取数,存数 nums_list.append(node.val) # node = node.next # print(node.val) node = node.next nums_list.append(node.val) # print(node.val) # print(lists) # print(nums_list) list_data = sorted(nums_list) # 排序 # print(list_data) for num in list_data: # 链表依次 链接 cur_node.next = ListNode(num) # 区分与C++,每次next要定义一个新的对象链表结点。 cur_node = cur_node.next # cur_node.next = None # print(cur_node) return res_head.next