Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit672133c

Browse files
committed
list
1 parent9649dc8 commit672133c

File tree

2 files changed

+122
-90
lines changed

2 files changed

+122
-90
lines changed

‎list/MergeKLists_1204.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
packageAlgorithms.list;
2+
importjava.util.Comparator;
3+
importjava.util.List;
4+
importjava.util.PriorityQueue;
5+
6+
importAlgorithms.algorithm.others.ListNode;
7+
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode(int x) {
14+
* val = x;
15+
* next = null;
16+
* }
17+
* }
18+
*/
19+
publicclassMergeKLists_1204 {
20+
/*
21+
SOL 1:
22+
使用merge sort和分治法完成
23+
*/
24+
publicListNodemergeKLists1(List<ListNode>lists) {
25+
// 记得加上这个合法性判断。
26+
if (lists ==null ||lists.size() ==0) {
27+
returnnull;
28+
}
29+
30+
returnhelper(lists,0,lists.size() -1);
31+
}
32+
33+
/*
34+
l, r表示list的左右边界
35+
*/
36+
publicListNodehelper(List<ListNode>lists,intl,intr) {
37+
if (l <r) {
38+
intmid =l + (r -l) /2;
39+
40+
/*
41+
分治法。把问题分为2个更小的子问题:左边list的merge,和右边list的merge.
42+
再把2个生成的解合并在一起。
43+
*/
44+
returnmerge(helper(lists,l,mid),helper(lists,mid +1,r));
45+
}
46+
47+
returnlists.get(l);
48+
}
49+
50+
publicListNodemerge(ListNoden1,ListNoden2) {
51+
ListNodedummy =newListNode(0);
52+
ListNodecur =dummy;
53+
54+
while (n1 !=null &&n2 !=null) {
55+
if (n1.val <n2.val) {
56+
cur.next =n1;
57+
n1 =n1.next;
58+
}else {
59+
cur.next =n2;
60+
n2 =n2.next;
61+
}
62+
63+
cur =cur.next;
64+
}
65+
66+
if (n1 !=null) {
67+
cur.next =n1;
68+
}else {
69+
cur.next =n2;
70+
}
71+
72+
returndummy.next;
73+
}
74+
75+
/*
76+
SOL 2:
77+
使用 priority Queue.
78+
*/
79+
publicListNodemergeKLists(List<ListNode>lists) {
80+
// 记得加上这个合法性判断。
81+
if (lists ==null ||lists.size() ==0) {
82+
returnnull;
83+
}
84+
85+
intsize =lists.size();
86+
87+
PriorityQueue<ListNode>q =newPriorityQueue<ListNode>(size,
88+
newComparator<ListNode>() {
89+
// 注意,此处参数用ListNode
90+
publicintcompare(ListNodeo1,ListNodeo2) {
91+
returno1.val -o2.val;
92+
}
93+
}
94+
);
95+
96+
// Add all the head node to the priority queue.
97+
for (ListNodenode:lists) {
98+
if (node !=null) {
99+
// Should skip the null node.s
100+
q.offer(node);
101+
}
102+
}
103+
104+
ListNodedummy =newListNode(0);
105+
ListNodetail =dummy;
106+
107+
while (!q.isEmpty()) {
108+
// get the smallest node from the queue.
109+
ListNodecur =q.poll();
110+
111+
tail.next =cur;
112+
tail =tail.next;
113+
114+
// 将下一个节点补充进来。
115+
if (cur.next !=null) {
116+
q.offer(cur.next);
117+
}
118+
}
119+
120+
returndummy.next;
121+
}
122+
}

‎list/SortList.java

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp