leetcode_【23】合并K个排序链表

1.题目描述

合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。

示例:

输入:
[
1->4->5,
1->3->4,
2->6
]
输出: 1->1->2->3->4->4->5->6

2.解题思路

方法1:

(1)利用priorityQueue的性质,每放一个数字进去就给你从小到大排好序了
(2)队列的性质是先进先出,故依次poll出来就是从小到大的顺序,将该数值放到listNode里面即可。

方法2:

借用合并两个链表的代码,依次将list[]分成两部分,在分别合并,执行用时为6ms

3.代码

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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode res = new ListNode(-1);
ListNode ans = res;
PriorityQueue<Integer> queue = new PriorityQueue<>();
//将所有节点的值放到一个queue里面从小到大排好序
for(int i = 0;i< lists.length;i++){
while(lists[i]!=null){
queue.add(lists[i].val);
lists[i] = lists[i].next;
}
}
//依次建立链表
while(queue.size()!=0){
res.next = new ListNode(queue.poll());
res = res.next;
}
return ans.next;
}

}

方法2:

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
public ListNode mergeKLists(ListNode[] lists){
if(lists.length == 0)
return null;
if(lists.length == 1)
return lists[0];
if(lists.length == 2){
return mergeTwoLists(lists[0],lists[1]);
}
//将一半的链表放到l1里面
int mid = lists.length/2;
ListNode[] l1 = new ListNode[mid];
for(int i = 0; i < mid; i++){
l1[i] = lists[i];
}
//将一半的链表放到l2里面
ListNode[] l2 = new ListNode[lists.length-mid];
for(int i = mid,j=0; i < lists.length; i++,j++){
l2[j] = lists[i];
}
//递归实现多个链表的排序
return mergeTwoLists(mergeKLists(l1),mergeKLists(l2));

}
//递归方式实现两个链表排序
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;

ListNode head = null;
if (l1.val <= l2.val){
head = l1;
head.next = mergeTwoLists(l1.next, l2);
} else {
head = l2;
head.next = mergeTwoLists(l1, l2.next);
}
return head;
}

4.提交记录

合并K个排序的链表

文章目录
  1. 1. 1.题目描述
  2. 2. 2.解题思路
  3. 3. 3.代码
  4. 4. 4.提交记录
| 139.6k