-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path148. Sort List.java
65 lines (65 loc) · 1.93 KB
/
148. Sort List.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
58
59
60
61
62
63
64
65
/**
* 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; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode mid_node = getMidOf(head);
ListNode start = head;
ListNode end = mid_node.next;
mid_node.next = null;
start = sortList(start);
end = sortList(end);
ListNode merged_list = mergeList(start, end);
return merged_list;
}
private ListNode mergeList(ListNode start, ListNode end) {
ListNode t1 = start;
ListNode t2 = end;
ListNode dummy = new ListNode(-1);
ListNode tmp = dummy;
while (t1 != null && t2 != null) {
int a = t1.val;
int b = t2.val;
if (a < b) {
tmp.next = t1;
t1 = t1.next;
} else {
tmp.next = t2;
t2 = t2.next;
}
tmp = tmp.next;
}
if (t1 != null) tmp.next = t1;
if (t2 != null) tmp.next = t2;
return dummy.next;
}
private ListNode getMidOf(ListNode head) {
ListNode slow_pointer = head;
ListNode fast_pointer = head;
while (fast_pointer.next != null && fast_pointer.next.next != null) {
slow_pointer = slow_pointer.next;
fast_pointer = fast_pointer.next.next;
}
return slow_pointer;
}
}