Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d34ae34

Browse files
update: 148
1 parent cf62e5c commit d34ae34

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
8888
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [JavaScript](./src/single-number/res.js) | Easy |
8989
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [JavaScript](./src/single-number-ii/res.js) | Medium |
9090
| 139 | [word-break](https://leetcode.com/problems/word-break/) | [TypeScript](./src/word-break/res.ts) | Medium |
91+
| 148 | [sort-list](https://leetcode.com/problems/sort-list/) | [TypeScript](./src/sort-list/res.ts) | Medium |
9192
| 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 |
9293
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [JavaScript](./src/maximum-product-subarray/res.js) | Medium |
9394
| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [JavaScript](./src/find-minimum-in-rotated-sorted-array/res.js) | Medium |

‎src/sort-list/res.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Definition for singly-linked list.
3+
*/
4+
class ListNode {
5+
val: number
6+
next: ListNode | null
7+
constructor(val?: number, next?: ListNode | null) {
8+
this.val = (val===undefined ? 0 : val)
9+
this.next = (next===undefined ? null : next)
10+
}
11+
}
12+
13+
function merge(start: ListNode | null, end: ListNode | null): ListNode | null {
14+
let result: ListNode | null = new ListNode(0);
15+
let resultPoint = result;
16+
while (start && end) {
17+
if (start.val > end.val) {
18+
resultPoint.next = end;
19+
end = end.next;
20+
} else {
21+
resultPoint.next = start;
22+
start = start.next;
23+
}
24+
25+
resultPoint = resultPoint.next;
26+
}
27+
28+
if (start) {
29+
resultPoint.next = start;
30+
} else {
31+
resultPoint.next = end;
32+
}
33+
34+
return result.next;
35+
}
36+
37+
function sortWithRange(start: ListNode | null, end: ListNode | null): ListNode | null {
38+
if (!start) {
39+
return start;
40+
}
41+
if (start.next === end) {
42+
start.next = null;
43+
return start;
44+
}
45+
46+
let midPoint: ListNode | null = start;
47+
let endPoint: ListNode | null = start;
48+
while(endPoint !== end) {
49+
midPoint = midPoint?.next;
50+
endPoint = endPoint?.next;
51+
52+
if (endPoint !== end) {
53+
endPoint = endPoint?.next;
54+
}
55+
}
56+
57+
const leftNode = sortWithRange(start, midPoint);
58+
const rightNode = sortWithRange(midPoint, end);
59+
60+
return merge(leftNode, rightNode);
61+
}
62+
63+
function sortList(head: ListNode | null): ListNode | null {
64+
return sortWithRange(head, null);
65+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /