-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_k_sorted_lists.py
More file actions
41 lines (32 loc) · 1.04 KB
/
merge_k_sorted_lists.py
File metadata and controls
41 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# https://leetcode.com/problems/merge-k-sorted-lists/
# Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
"""
>>> list1 = LinkedList([1, 4, 5])
>>> list2 = LinkedList([1, 3, 4])
>>> list3 = LinkedList([2, 6])
>>> lists = [list1.head, list2.head, list3.head]
>>> merged_sorted_list = Solution().mergeKLists(lists)
>>> LinkedList().print(merged_sorted_list)
1->1->2->3->4->4->5->6
"""
import heapq
from typing import List
from datastructs import LinkedList
# 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:
min_heap = []
for list in lists:
while list:
heapq.heappush(min_heap, list.val)
list = list.next
head = ListNode(-1)
current = head
while min_heap:
current.next = ListNode(heapq.heappop(min_heap))
current = current.next
return head.next