[LeetCode] 23. Merge k Sorted Lists

김민우·2023년 3월 12일
0

알고리즘

목록 보기
155/189

- Problem

23. Merge k Sorted Lists


- 내 풀이

class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        heap = []

        for l in lists:
            while l:
                heappush(heap, l.val)
                l = l.next
        
        if not heap:
            return
        
        head = tail = ListNode(heappop(heap))

        while heap:
            tail.next = ListNode(heappop(heap))
            tail = tail.next

        return head

- 결과

profile
Pay it forward.

0개의 댓글