-
Notifications
You must be signed in to change notification settings - Fork 56
/
Solution.java
57 lines (54 loc) · 1.72 KB
/
Solution.java
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
package g0101_0200.s0148_sort_list;
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Sorting #Two_Pointers #Linked_List
// #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Big_O_Time_O(log(N))_Space_O(log(N))
// #2022_06_24_Time_12_ms_(85.82%)_Space_76_MB_(43.84%)
import com_github_leetcode.ListNode;
/*
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
@SuppressWarnings("java:S135")
public class Solution {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head;
ListNode fast = head;
ListNode pre = slow;
while (fast != null && fast.next != null) {
pre = slow;
slow = slow.next;
fast = fast.next.next;
}
pre.next = null;
ListNode first = sortList(head);
ListNode second = sortList(slow);
ListNode res = new ListNode(1);
ListNode cur = res;
while (first != null || second != null) {
if (first == null) {
cur.next = second;
break;
} else if (second == null) {
cur.next = first;
break;
} else if (first.val <= second.val) {
cur.next = first;
first = first.next;
cur = cur.next;
} else {
cur.next = second;
second = second.next;
cur = cur.next;
}
}
return res.next;
}
}