题目:
合并K个排序链表:合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
思路:
暴力法
程序:
# 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:
length = len(lists)
if length <= 0:
return None
myList = []
for index in range(length):
myLinkedList = lists[index]
while myLinkedList:
myList.append(myLinkedList.val)
myLinkedList = myLinkedList.next
myList.sort()
myNewHead = ListNode(0)
myNewNode = myNewHead
for index2 in myList:
myNewNode2 = ListNode(index2)
myNewNode.next = myNewNode2
myNewNode = myNewNode2
return myNewHead.next