Skip to content

Commit 353f795

Browse files
committed
new solution for lc147
1 parent 2ae4a0f commit 353f795

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

LinkedList.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ No.|Title|Difficulty|Solved|Date
1717
141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)|Easy|yes|2019-01-09
1818
142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)|Medium|yes
1919
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
2121
206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)|Easy|yes|2019-01-10
2222
234|[Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)|Easy|yes|2019-01-10
2323
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) {
130130

131131
147. [Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)
132132

133+
插入排序。可以使用递归方法解决,也可以使用迭代方法解决。
133134

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+
```
134171

135172
206. [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)
136173

src/main/java/medium/lc147.java

+35
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,39 @@ private ListNode insertion(ListNode head, ListNode node) {
5252
}
5353
return head;
5454
}
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+
}
5590
}

0 commit comments

Comments
 (0)