-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode148.java
More file actions
67 lines (61 loc) · 1.92 KB
/
LeetCode148.java
File metadata and controls
67 lines (61 loc) · 1.92 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
import util.ListNode;
public class LeetCode148 {
public static void main(String[] args) {
// 输入:head = [4,2,1,3]
// 输出:[1,2,3,4]
System.out.println(new Solution148().sortList(ListNode.buildLinkedList(new Integer[] { 4, 2, 1, 3 })));
// 输入:head = [-1,5,3,4,0]
// 输出:[-1,0,3,4,5]
System.out.println(new Solution148().sortList(ListNode.buildLinkedList(new Integer[] { -1, 5, 3, 4, 0 })));
// 输入:head = []
// 输出:[]
System.out.println(new Solution148().sortList(ListNode.buildLinkedList(new Integer[] {})));
}
}
class Solution148 {
public ListNode sortList(ListNode head) {
return sort(head);
}
public ListNode sort(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode l1 = head;
ListNode l2 = slow.next;
slow.next = null;
l1 = sort(l1);
l2 = sort(l2);
return merge(l1, l2);
}
public ListNode merge(ListNode l1, ListNode l2) {
ListNode dumNode = new ListNode();
ListNode p1 = l1;
ListNode p2 = l2;
ListNode node = dumNode;
while (p1 != null || p2 != null) {
if (p1 == null) {
node.next = p2;
p2 = p2.next;
} else if (p2 == null) {
node.next = p1;
p1 = p1.next;
} else {
if (p1.val <= p2.val) {
node.next = p1;
p1 = p1.next;
} else {
node.next = p2;
p2 = p2.next;
}
}
node = node.next;
}
return dumNode.next;
}
}