-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT127.java
More file actions
76 lines (66 loc) · 1.9 KB
/
T127.java
File metadata and controls
76 lines (66 loc) · 1.9 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.ArrayList;
/**
* @Author:Aliyang
* @Data: Created in 下午10:43 18-6-16
* merge-k-sorted-lists:别人解法
* 思路:使用归并排序,递归分治
**/
public class T127 {
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public ListNode mergeKLists(ArrayList<ListNode> lists) {
return partion(lists,0,lists.size()-1);
}
// 将整个链表数组进行递归分治
public static ListNode partion(ArrayList<ListNode> lists,int s,int e){
if(s==e) return lists.get(s);
if(s<e){
int q=(s+e)/2;
ListNode l1=partion(lists,s,q);
ListNode l2=partion(lists,q+1,e);
return merge(l1,l2);
}else
return null;
}
// 对两个链表进行合并
public static ListNode merge(ListNode l1,ListNode l2){
if(l1==null) return l2;
if(l2==null) return l1;
if(l1.val<l2.val){
l1.next=merge(l1.next,l2);
return l1;//谁的值小返回哪个链表
}else{
l2.next=merge(l1,l2.next);
return l2;
}
}
public static void main(String[] args){
T127 t=new T127();
ArrayList<ListNode> lists=new ArrayList<>();
ListNode head=new ListNode(1);
ListNode realHead=head;
head.next=new ListNode(2);
head=head.next;
head.next=new ListNode(6);
head=head.next;
lists.add(realHead);
ListNode head1=new ListNode(3);
ListNode realHead1=head1;
head1.next=new ListNode(3);
head1=head1.next;
head1.next=new ListNode(5);
head1=head1.next;
lists.add(realHead1);
ListNode res=t.mergeKLists(lists);
while (res!=null){
System.out.print(res.val+",");
res=res.next;
}
}
}