You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: data_structures/linked_list/merge_k_sorted_list.py
+21Lines changed: 21 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,21 @@
1
+
""" This algorithm merges k sorted linked lists into a single sorted linked list.
2
+
3
+
Each input linked list is assumed to be sorted in non-decreasing order. The algorithm uses a min-heap (priority queue) to efficiently determine the next smallest node among all the heads of the lists.
4
+
5
+
At every step:
6
+
7
+
The smallest element among the current nodes of all lists is extracted from the heap.
8
+
9
+
That node is appended to the result list.
10
+
11
+
The next node from the same list (if any) is then pushed into the heap.
12
+
13
+
This process repeats until all nodes from all lists have been processed.
14
+
The final result is a single sorted linked list containing all elements from the input lists.
0 commit comments