Skip to content

Commit 38a3c48

Browse files
committed
update: 147
1 parent b485a05 commit 38a3c48

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
6464
| 77 | [combinations](https://leetcode.com/problems/combinations/) | [TypeScript](./src/combinations/res.ts) | Medium |
6565
| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | [JavaScript](./src/remove-duplicates-from-sorted-array-ii/res.js) | Medium |
6666
| 81 | [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii/) | [JavaScript](./src/search-in-rotated-sorted-array-ii/res.js) | Medium |
67-
| 88 | [ Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [JavaScript](./src/merge-sorted-array/res.js) | Medium |
67+
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [JavaScript](./src/merge-sorted-array/res.js) | Medium |
6868
| 91 | [Decode Ways](https://leetcode.com/problems/decode-ways/) | [JavaScript](./src/decode-ways/res.js) | Medium |
6969
| 93 | [Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/) | [JavaScript](./src/restore-ip-addresses/res.js) | Medium |
7070
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [TypeScript](./src/binary-tree-inorder-traversal/res.ts) | Easy |
@@ -100,6 +100,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
100100
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [JavaScript](./src/single-number-ii/res.js) | Medium |
101101
| 139 | [word-break](https://leetcode.com/problems/word-break/) | [TypeScript](./src/word-break/res.ts) | Medium |
102102
| 143 | [reorder-list](https://leetcode.com/problems/reorder-list/) | [TypeScript](./src/reorder-list/res.ts) | Medium |
103+
| 147 | [insertion-sort-list](https://leetcode.com/problems/insertion-sort-list/) | [TypeScript](./src/insertion-sort-list/res.ts) | Medium |
103104
| 148 | [sort-list](https://leetcode.com/problems/sort-list/) | [TypeScript](./src/sort-list/res.ts) | Medium |
104105
| 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [JavaScript](./src/reverse-words-in-a-string/res.js) | Medium |
105106
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [JavaScript](./src/maximum-product-subarray/res.js) | Medium |

src/insertion-sort-list/res.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function insertionSortList(head: ListNode | null): ListNode | null {
14+
if (!head) {
15+
return null;
16+
}
17+
18+
const dumpyHead = new ListNode(0);
19+
let lastSortedNode = head;
20+
dumpyHead.next = head;
21+
22+
while (lastSortedNode?.next) {
23+
const nextNode = lastSortedNode.next;
24+
if (nextNode.val >= lastSortedNode.val) {
25+
lastSortedNode = nextNode;
26+
} else {
27+
let currentIterateNode = dumpyHead;
28+
while (currentIterateNode.next && currentIterateNode.next.val < nextNode.val) {
29+
currentIterateNode = currentIterateNode.next;
30+
}
31+
32+
// link
33+
lastSortedNode.next = nextNode.next;
34+
35+
// swap
36+
const swapNode = currentIterateNode.next;
37+
currentIterateNode.next = nextNode;
38+
nextNode.next = swapNode;
39+
lastSortedNode = swapNode as ListNode;
40+
}
41+
}
42+
43+
return dumpyHead.next;
44+
};

0 commit comments

Comments
 (0)