Leetcode - 23. Merge k Sorted Lists
Coding Test

Leetcode - 23. Merge k Sorted Lists

일시불

답안

먼저 노드 값을 가지고 정렬을 한 다음(여기서는 heap 을 이용), 순서대로 노드를 만들어 붙여주면 된다.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
import heapq

class Solution:
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        heap_ = []
        for node in lists:
            while node:
                heapq.heappush(heap_, node.val)
                node = node.next

        start = res_node = ListNode()
        while heap_:
            res_node.next = ListNode(heapq.heappop(heap_))
            res_node = res_node.next
            
        return start.next