原题链接
思路
把数组中的链表看成是 若干队列, 我们的目的是得到一个升序的链表, 所以每次遍历我们只对队首元素最小的队列进行poll操作, 再将其拼接到我们结果队列的尾部即可
贴个代码
public class Solution {
public ListNode mergeKLists (ListNode[] lists) {
boolean isEnpty = false;
ListNode head = new ListNode(0);
ListNode cur = head;
while(!isEnpty) {
int min = 50001;
int index = 0;
isEnpty = true;
for(int i = 0; i < lists.length; i++) {
if(lists[i] != null) {
isEnpty = false;
if(lists[i].val < min) {
min = lists[i].val;
index = i;
}
}
}
if(!isEnpty) {
cur.next = lists[index];
cur = cur.next;
lists[index] = lists[index].next;
}
}
return head.next;
}
}
具体代码参上
好的!本次分享到这就结束了
如果对铁汁你有帮助的话,记得点赞👍+收藏⭐️+关注➕
我在这先行拜谢了:)