File tree 2 files changed +73
-1
lines changed
2 files changed +73
-1
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ No.|Title|Difficulty|Solved|Date
17
17
141|[ Linked List Cycle] ( https://leetcode.com/problems/linked-list-cycle/ ) |Easy|yes|2019-01-09
18
18
142|[ Linked List Cycle II] ( https://leetcode.com/problems/linked-list-cycle-ii/ ) |Medium|yes
19
19
143|[ Reorder List] ( https://leetcode.com/problems/reorder-list/ ) |Medium|yes|2019-01-12
20
- 147|[ Insertion Sort List] ( https://leetcode.com/problems/insertion-sort-list/ ) |Medium|yes|
20
+ 147|[ Insertion Sort List] ( https://leetcode.com/problems/insertion-sort-list/ ) |Medium|yes|2019-01-14
21
21
206|[ Reverse Linked List] ( https://leetcode.com/problems/reverse-linked-list/ ) |Easy|yes|2019-01-10
22
22
234|[ Palindrome Linked List] ( https://leetcode.com/problems/palindrome-linked-list/ ) |Easy|yes|2019-01-10
23
23
237|[ Delete Node in a Linked List] ( https://leetcode.com/problems/delete-node-in-a-linked-list/ ) |Easy|yes|2019-01-10
@@ -130,7 +130,44 @@ public void reorderList2(ListNode head) {
130
130
131
131
147 . [ Insertion Sort List] ( https://leetcode.com/problems/insertion-sort-list/ )
132
132
133
+ 插入排序。可以使用递归方法解决,也可以使用迭代方法解决。
133
134
135
+ ``` java
136
+ public ListNode insertionSortList(ListNode head) {
137
+ if (head == null ) return null ;
138
+ ListNode h = new ListNode (0 );
139
+ h. next = head;
140
+ ListNode cur = head. next;
141
+ head. next = null ;
142
+ // tail node
143
+ ListNode prev2 = head;
144
+ while (cur != null ) {
145
+ ListNode prev = h;
146
+ ListNode current = h. next;
147
+ while (current != null ) {
148
+ if (current. val <= cur. val) {
149
+ prev = current;
150
+ current = current. next;
151
+ } else {
152
+ break ;
153
+ }
154
+ }
155
+ if (current == null ) {
156
+ prev. next = cur;
157
+ prev2 = cur;
158
+ cur = cur. next;
159
+ prev2. next = null ;
160
+ } else {
161
+ ListNode temp = cur. next;
162
+ prev. next = cur;
163
+ cur. next = current;
164
+ cur = temp;
165
+ }
166
+
167
+ }
168
+ return h. next;
169
+ }
170
+ ```
134
171
135
172
206 . [ Reverse Linked List] ( https://leetcode.com/problems/reverse-linked-list/ )
136
173
Original file line number Diff line number Diff line change @@ -52,4 +52,39 @@ private ListNode insertion(ListNode head, ListNode node) {
52
52
}
53
53
return head ;
54
54
}
55
+
56
+ // a new solution
57
+ public ListNode insertionSortList2 (ListNode head ) {
58
+ if (head == null ) return null ;
59
+ ListNode h = new ListNode (0 );
60
+ h .next = head ;
61
+ ListNode cur = head .next ;
62
+ head .next = null ;
63
+ ListNode prev2 = head ;
64
+ while (cur != null ) {
65
+ ListNode prev = h ;
66
+ ListNode current = h .next ;
67
+ while (current != null ) {
68
+ if (current .val <= cur .val ) {
69
+ prev = current ;
70
+ current = current .next ;
71
+ } else {
72
+ break ;
73
+ }
74
+ }
75
+ if (current == null ) {
76
+ prev .next = cur ;
77
+ prev2 = cur ;
78
+ cur = cur .next ;
79
+ prev2 .next = null ;
80
+ } else {
81
+ ListNode temp = cur .next ;
82
+ prev .next = cur ;
83
+ cur .next = current ;
84
+ cur = temp ;
85
+ }
86
+
87
+ }
88
+ return h .next ;
89
+ }
55
90
}
You can’t perform that action at this time.
0 commit comments