반응형
leetcode.com/problems/merge-k-sorted-lists/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
def toNode(lst):
head = None
while lst:
head = ListNode(lst.pop(),head)
return head
def toList(head):
lst = []
while head:
lst.append(head.val)
head = head.next
return lst
lst= []
for node in lists:
lst += toList(node)
lst.sort()
return toNode(lst)
댓글