Skip to content

Commit c754381

Browse files
committed
fd
1 parent 21a083b commit c754381

File tree

5 files changed

+99
-35
lines changed

5 files changed

+99
-35
lines changed

solution/src/main/java/com/inuker/solution/InsertionSortList.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,22 @@
88

99
public class InsertionSortList {
1010

11+
/**
12+
* 其实很简单,就是从head中每次取一个节点,插到dummy中适当的位置
13+
*/
1114
public ListNode insertionSortList(ListNode head) {
12-
if (head == null) {
13-
return head;
14-
}
15-
16-
ListNode helper = new ListNode(0); //new starter of the sorted list
17-
ListNode cur = head; //the node will be inserted
18-
ListNode pre = helper; //insert node between pre and pre.next
19-
ListNode next = null; //the next node will be inserted
20-
//not the end of input list
21-
while (cur != null) {
22-
next = cur.next;
23-
//find the right place to insert
24-
while (pre.next != null && pre.next.val < cur.val) {
25-
pre = pre.next;
15+
ListNode dummy = new ListNode(0);
16+
for (ListNode next; head != null; head = next) {
17+
next = head.next;
18+
for (ListNode cur = dummy; cur != null; cur = cur.next) {
19+
if (cur.next != null && head.val > cur.next.val) {
20+
continue;
21+
}
22+
head.next = cur.next;
23+
cur.next = head;
24+
break;
2625
}
27-
//insert between pre and pre.next
28-
cur.next = pre.next;
29-
pre.next = cur;
30-
pre = helper;
31-
cur = next;
3226
}
33-
34-
return helper.next;
27+
return dummy.next;
3528
}
3629
}

solution/src/main/java/com/inuker/solution/MaximumSubarray.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
public class MaximumSubarray {
88

9+
// dp[i]表示包含第i个元素时的最大和
910
public int maxSubArray(int[] nums) {
1011
if (nums.length == 0) {
1112
return 0;

solution/src/main/java/com/inuker/solution/RecoverBinarySearchTree.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public void recoverTree(TreeNode root) {
1919
second.val = val;
2020
}
2121

22+
// Morris遍历
2223
private void inorder(TreeNode root) {
2324
while (root != null) {
2425
if (root.left == null) {

solution/src/main/java/com/inuker/solution/SortList.java

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66
* Created by liwentian on 2017/9/11.
77
*/
88

9+
/**
10+
* 这题采用merge sort,可自顶向下或自底向上
11+
* 区别是自顶向下是递归的,需要额外的空间
12+
* 而自底向上不用
13+
* 时间复杂度都是O(nlgn)
14+
*/
915
public class SortList {
1016

17+
/**
18+
* 自顶向下
19+
*/
1120
public ListNode sortList(ListNode head) {
1221
if (head == null || head.next == null)
1322
return head;
@@ -27,26 +36,70 @@ public ListNode sortList(ListNode head) {
2736
}
2837

2938
ListNode merge(ListNode l1, ListNode l2) {
30-
ListNode l = new ListNode(0), p = l;
31-
32-
while (l1 != null && l2 != null) {
39+
ListNode dummy = new ListNode(0), cur = dummy;
40+
for ( ; l1 != null && l2 != null; ) {
3341
if (l1.val < l2.val) {
34-
p.next = l1;
42+
cur.next = l1;
3543
l1 = l1.next;
36-
} else {
37-
p.next = l2;
44+
} else {
45+
cur.next = l2;
3846
l2 = l2.next;
3947
}
40-
p = p.next;
48+
cur = cur.next;
4149
}
50+
cur.next = l1 != null ? l1 : l2;
51+
return dummy.next;
52+
}
4253

43-
if (l1 != null)
44-
p.next = l1;
45-
46-
if (l2 != null)
47-
p.next = l2;
54+
/**
55+
* 自底向上
56+
*/
57+
public ListNode sortList2(ListNode head) {
58+
int size = 0;
59+
for (ListNode node = head; node != null; node = node.next, size++);
60+
ListNode dummy = new ListNode(0);
61+
ListNode left, right, cur, tail;
62+
for (int step = 1; step <= size; step *= 2) {
63+
for (cur = head, tail = dummy; cur != null; ) {
64+
left = cur;
65+
right = split(left, step);
66+
cur = split(right, step);
67+
tail = merge(left, right, tail);
68+
}
69+
head = dummy.next;
70+
}
71+
return dummy.next;
72+
}
4873

49-
return l.next;
74+
/**
75+
* 将l1和l2合并后挂在tail下,返回新的tail
76+
*/
77+
ListNode merge(ListNode l1, ListNode l2, ListNode tail) {
78+
for ( ; l1 != null && l2 != null; ) {
79+
if (l1.val < l2.val) {
80+
tail.next = l1;
81+
l1 = l1.next;
82+
} else {
83+
tail.next = l2;
84+
l2 = l2.next;
85+
}
86+
tail = tail.next;
87+
}
88+
tail.next = l1 != null ? l1 : l2;
89+
for ( ; tail.next != null; tail = tail.next);
90+
return tail;
5091
}
5192

93+
/**
94+
* 从start开始取step个节点,返回下一个节点
95+
*/
96+
private ListNode split(ListNode start, int step) {
97+
for (step--; start != null && step > 0; step--, start = start.next);
98+
if (start != null && start.next != null) {
99+
ListNode next = start.next;
100+
start.next = null;
101+
return next;
102+
}
103+
return null;
104+
}
52105
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.inuker.test;
22

3+
import com.leetcode.library.ListNode;
34
import com.leetcode.library.TreeNode;
45

56
import java.util.HashMap;
@@ -21,8 +22,23 @@
2122
public class main {
2223

2324
public static void main(String[] args) {
24-
2525
}
2626

2727

28+
public ListNode insertionSortList(ListNode head) {
29+
ListNode dummy = new ListNode(0);
30+
for (ListNode next; head != null; head = next) {
31+
next = head.next;
32+
for (ListNode cur = dummy; cur != null; cur = cur.next) {
33+
if (cur.next != null && head.val > cur.next.val) {
34+
continue;
35+
}
36+
head.next = cur.next;
37+
cur.next = head;
38+
break;
39+
}
40+
}
41+
return dummy.next;
42+
}
43+
2844
}

0 commit comments

Comments
 (0)