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
* 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
+
importjava.util.PriorityQueue;
57
+
58
+
publicclassSolution {
59
+
publicListNodemergeKLists(ListNode[] lists) {
60
+
PriorityQueue<ListNode> minHeap =newPriorityQueue<>((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 =newListNode(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
+
publicstaticvoidmain(String[] args) {
88
+
Solution solution =newSolution();
89
+
90
+
// Test case
91
+
ListNode[] lists =newListNode[] {
92
+
ListNode.createList(newint[] {1, 4, 5}),
93
+
ListNode.createList(newint[] {1, 3, 4}),
94
+
ListNode.createList(newint[] {2, 6})
95
+
};
96
+
System.out.println("Merged list:");
97
+
ListNode.printList(solution.mergeKLists(lists));
98
+
}
99
+
}
100
+
101
+
classListNode {
102
+
int val;
103
+
ListNode next;
104
+
105
+
ListNode(intval) {
106
+
this.val = val;
107
+
}
108
+
109
+
staticListNodecreateList(int[] arr) {
110
+
if (arr ==null|| arr.length ==0) {
111
+
returnnull;
112
+
}
113
+
114
+
ListNode dummy =newListNode(0);
115
+
ListNode current = dummy;
116
+
for (int num : arr) {
117
+
current.next =newListNode(num);
118
+
current = current.next;
119
+
}
120
+
return dummy.next;
121
+
}
122
+
123
+
staticvoidprintList(ListNodehead) {
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