-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge K sorted Lists.py
More file actions
65 lines (52 loc) · 1.97 KB
/
Merge K sorted Lists.py
File metadata and controls
65 lines (52 loc) · 1.97 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#https://leetcode.com/problems/merge-k-sorted-lists/solution/
#O(N*K solution similar to the merge sort procedure!)
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
newList=ListNode(None)
headNewList=newList
k=len(lists)
if(k==0):
return None
while True:
min_element=float("inf")
min_list_index=None
for i in range(k):
currentNode=lists[i]
if(currentNode!=None):
if(currentNode.val<min_element):
min_element=currentNode.val
min_list_index=i
if(min_list_index!=None):
newNode=ListNode(min_element)
newList.next=newNode
newList=newList.next
lists[min_list_index]=lists[min_list_index].next
else:
break
return headNewList.next
#https://leetcode.com/problems/merge-k-sorted-lists/solution/
#Using Priority Queue (Min-Heap)
import heapq
class Solution:
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
newList=ListNode(None)
headNewList=newList
k=len(lists)
if(k==0):
return None
n=lists[0]
heap=[]
heapq.heapify(heap)
for index,node in enumerate(lists):
if(node):
heapq.heappush(heap,[node.val,index])
while heap:
minNodeValue,minNodeIndex=heapq.heappop(heap)
newNode=ListNode(minNodeValue)
newList.next=newNode
newList=newList.next
if(lists[minNodeIndex].next):
#Add the next node to heap
lists[minNodeIndex]=lists[minNodeIndex].next
heapq.heappush(heap,[lists[minNodeIndex].val,minNodeIndex])
return headNewList.next