Skip to content

Commit 53b3ca0

Browse files
authored
Update README with merge k sorted lists solution
Added detailed explanation and implementation for merging k sorted lists using a priority queue.
1 parent c27a427 commit 53b3ca0

File tree

1 file changed

+98
-1
lines changed
  • src/main/java/g0001_0100/s0023_merge_k_sorted_lists

1 file changed

+98
-1
lines changed

src/main/java/g0001_0100/s0023_merge_k_sorted_lists/readme.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,101 @@ _Merge all the linked-lists into one sorted linked-list and return it._
3333
* `0 <= lists[i].length <= 500`
3434
* <code>-10<sup>4</sup> <= lists[i][j] <= 10<sup>4</sup></code>
3535
* `lists[i]` is sorted in **ascending order**.
36-
* The sum of `lists[i].length` will not exceed <code>10<sup>4</sup></code>.
36+
* The sum of `lists[i].length` will not exceed <code>10<sup>4</sup></code>.
37+
38+
To solve the "Merge k Sorted Lists" problem in Java with a `Solution` class, we can use a priority queue (min-heap) to efficiently merge the lists. Here are the steps:
39+
40+
1. Define a `Solution` class.
41+
2. Define a method named `mergeKLists` that takes an array of linked lists `lists` as input and returns a single sorted linked list.
42+
3. Create a priority queue of ListNode objects. We will use this priority queue to store the heads of each linked list.
43+
4. Iterate through each linked list in the input array `lists` and add the head node of each list to the priority queue.
44+
5. Create a dummy ListNode object to serve as the head of the merged sorted linked list.
45+
6. Initialize a ListNode object named `current` to point to the dummy node.
46+
7. While the priority queue is not empty:
47+
- Remove the ListNode with the smallest value from the priority queue.
48+
- Add this node to the merged linked list by setting the `next` pointer of the `current` node to this node.
49+
- Move the `current` pointer to the next node in the merged linked list.
50+
- If the removed node has a `next` pointer, add the next node from the same list to the priority queue.
51+
8. Return the `next` pointer of the dummy node, which points to the head of the merged sorted linked list.
52+
53+
Here's the implementation:
54+
55+
```java
56+
import java.util.PriorityQueue;
57+
58+
public class Solution {
59+
public ListNode mergeKLists(ListNode[] lists) {
60+
PriorityQueue<ListNode> minHeap = new PriorityQueue<>((a, b) -> a.val - b.val);
61+
62+
// Add the heads of all lists to the priority queue
63+
for (ListNode node : lists) {
64+
if (node != null) {
65+
minHeap.offer(node);
66+
}
67+
}
68+
69+
// Create a dummy node to serve as the head of the merged list
70+
ListNode dummy = new ListNode(0);
71+
ListNode current = dummy;
72+
73+
// Merge the lists
74+
while (!minHeap.isEmpty()) {
75+
ListNode minNode = minHeap.poll();
76+
current.next = minNode;
77+
current = current.next;
78+
79+
if (minNode.next != null) {
80+
minHeap.offer(minNode.next);
81+
}
82+
}
83+
84+
return dummy.next;
85+
}
86+
87+
public static void main(String[] args) {
88+
Solution solution = new Solution();
89+
90+
// Test case
91+
ListNode[] lists = new ListNode[] {
92+
ListNode.createList(new int[] {1, 4, 5}),
93+
ListNode.createList(new int[] {1, 3, 4}),
94+
ListNode.createList(new int[] {2, 6})
95+
};
96+
System.out.println("Merged list:");
97+
ListNode.printList(solution.mergeKLists(lists));
98+
}
99+
}
100+
101+
class ListNode {
102+
int val;
103+
ListNode next;
104+
105+
ListNode(int val) {
106+
this.val = val;
107+
}
108+
109+
static ListNode createList(int[] arr) {
110+
if (arr == null || arr.length == 0) {
111+
return null;
112+
}
113+
114+
ListNode dummy = new ListNode(0);
115+
ListNode current = dummy;
116+
for (int num : arr) {
117+
current.next = new ListNode(num);
118+
current = current.next;
119+
}
120+
return dummy.next;
121+
}
122+
123+
static void printList(ListNode head) {
124+
while (head != null) {
125+
System.out.print(head.val + " ");
126+
head = head.next;
127+
}
128+
System.out.println();
129+
}
130+
}
131+
```
132+
133+
This implementation provides a solution to the "Merge k Sorted Lists" problem in Java using a priority queue.

0 commit comments

Comments
 (0)