Repository navigation
Expand file tree
/
Copy path148_Sort_List.java
More file actions
73 lines (68 loc) · 1.96 KB
/
Copy path148_Sort_List.java
File metadata and controls
73 lines (68 loc) · 1.96 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
/*
* 148. Sort List
* Target: Sort a linked list in O(n log n) time using constant space complexity.
* Difficulty:Medium
* Classification:Linked List, Sort
*/
/*
* Solution 1
* 2019-08-01 Runtime: 6 ms
* Algorithm: => Left and Right product lists.
* Time Complexity: O(N). Space Conplexity: O(N) where N represents the number of elements in the input array.
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
int n = 0;
while (head != null) {
head = head.next;
n++;
}
for (int i = 1; i < n; i <<= 1) {
ListNode pre = dummy;
ListNode cur = dummy.next;
while (cur != null) {
ListNode left = cur;
ListNode right = split(left, i);
cur = split(right, i);
pre = merge(left, right, pre);
}
}
return dummy.next;
}
private ListNode split(ListNode head, int i) {
if (head == null) return null;
for (int j = 1; head.next != null && j < i; j++) {
head = head.next;
}
ListNode right = head.next;
head.next = null;
return right;
}
private ListNode merge(ListNode left, ListNode right, ListNode pre) {
ListNode cur = pre;
while (left != null && right != null) {
if (left.val < right.val) {
cur.next = left;
left = left.next;
} else {
cur.next = right;
right = right.next;
}
cur = cur.next;
}
if (left != null) cur.next = left;
else if (right != null) cur.next = right;
while (cur.next != null) cur = cur.next;
return cur;
}
}