Skip to content

Commit b1ebb0b

Browse files
committed
update: 143
1 parent 98f419f commit b1ebb0b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
9090
| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [JavaScript](./src/single-number/res.js) | Easy |
9191
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [JavaScript](./src/single-number-ii/res.js) | Medium |
9292
| 139 | [word-break](https://leetcode.com/problems/word-break/) | [TypeScript](./src/word-break/res.ts) | Medium |
93+
| 143 | [reorder-list](https://leetcode.com/problems/reorder-list/) | [TypeScript](./src/reorder-list/res.ts) | Medium |
9394
| 148 | [sort-list](https://leetcode.com/problems/sort-list/) | [TypeScript](./src/sort-list/res.ts) | Medium |
9495
| 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 |
9596
| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [JavaScript](./src/maximum-product-subarray/res.js) | Medium |

‎src/reorder-list/res.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
/**
14+
Do not return anything, modify head in-place instead.
15+
*/
16+
function reorderList(head: ListNode | null): void {
17+
if (!head?.next?.next) {
18+
return ;
19+
}
20+
21+
let prevTailNode = head;
22+
let tailNode = head;
23+
while (tailNode.next) {
24+
prevTailNode = tailNode;
25+
tailNode = tailNode.next;
26+
}
27+
28+
const restNodes = head.next;
29+
prevTailNode.next = null;
30+
head.next = tailNode;
31+
tailNode.next = restNodes;
32+
reorderList(restNodes);
33+
34+
return ;
35+
};

0 commit comments

Comments
 (0)