Skip to content

Commit c4f869c

Browse files
committed
solve problem Insertion Sort List
1 parent 3c4ded7 commit c4f869c

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ All solutions will be accepted!
253253
|143|[Reorder List](https://leetcode-cn.com/problems/reorder-list/description/)|[java/py/js](./algorithms/ReorderList)|Medium|
254254
|515|[Find Largest Value In Each Tree Row](https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/description/)|[java/py/js](./algorithms/FindLargestValueInEachTreeRow)|Medium|
255255
|199|[Binary Tree Right Side View](https://leetcode-cn.com/problems/binary-tree-right-side-view/description/)|[java/py/js](./algorithms/BinaryTreeRightSideView)|Medium|
256+
|147|[Insertion Sort List](https://leetcode-cn.com/problems/insertion-sort-list/description/)|[java/py/js](./algorithms/InsertionSortList)|Medium|
256257

257258
# Database
258259
|#|Title|Solution|Difficulty|
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Insertion Sort List
2+
We can solve this problem by dummy node
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public ListNode insertionSortList(ListNode head) {
11+
ListNode dummy = new ListNode(-1);
12+
dummy.next = head;
13+
ListNode cur = dummy.next,
14+
pre = cur;
15+
16+
while (cur != null) {
17+
if (cur.val < pre.val) {
18+
ListNode next = cur.next;
19+
pre.next = next;
20+
21+
ListNode p1 = dummy,
22+
p2 = p1.next;
23+
24+
while (p2.val < cur.val) {
25+
p1 = p1.next;
26+
p2 = p2.next;
27+
}
28+
cur.next = p2;
29+
p1.next = cur;
30+
31+
cur = next;
32+
} else {
33+
pre = cur;
34+
cur = cur.next;
35+
}
36+
}
37+
38+
return dummy.next;
39+
}
40+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var insertionSortList = function(head) {
13+
let dummy = new ListNode(-1)
14+
dummy.next = head
15+
16+
let cur = dummy.next,
17+
pre = cur
18+
19+
while (cur) {
20+
if (cur.val < pre.val) {
21+
let next = cur.next
22+
pre.next = next
23+
24+
let p1 = dummy,
25+
p2 = dummy.next
26+
27+
while (p2.val < cur.val) {
28+
p1 = p1.next
29+
p2 = p2.next
30+
}
31+
cur.next = p2
32+
p1.next = cur
33+
34+
cur = next
35+
} else {
36+
pre = cur
37+
cur = cur.next
38+
}
39+
}
40+
41+
return dummy.next
42+
};
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def insertionSortList(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: ListNode
12+
"""
13+
dummy = ListNode(-1)
14+
dummy.next = head
15+
16+
cur = dummy.next
17+
pre = cur
18+
19+
while cur:
20+
if cur.val < pre.val:
21+
# remove cur
22+
nxt = cur.next
23+
pre.next = nxt
24+
25+
# insert cur to front
26+
p1 = dummy
27+
p2 = p1.next
28+
while p2.val < cur.val:
29+
p1 = p1.next
30+
p2 = p2.next
31+
cur.next = p2
32+
p1.next = cur
33+
34+
cur = nxt
35+
else:
36+
pre = cur
37+
cur = cur.next
38+
39+
return dummy.next

0 commit comments

Comments
 (0)