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 500d7cf

Browse files
feat: add typescript solution to lc problem: No.2074 (doocs#616)
No.2074.Reverse Nodes in Even Length Groups
1 parent a1cb3c9 commit 500d7cf

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

‎solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,49 @@ class Solution {
190190
}
191191
```
192192

193+
### **TypeScript**
194+
195+
```ts
196+
/**
197+
* Definition for singly-linked list.
198+
* class ListNode {
199+
* val: number
200+
* next: ListNode | null
201+
* constructor(val?: number, next?: ListNode | null) {
202+
* this.val = (val===undefined ? 0 : val)
203+
* this.next = (next===undefined ? null : next)
204+
* }
205+
* }
206+
*/
207+
208+
function reverseEvenLengthGroups(head: ListNode | null): ListNode | null {
209+
let nums = [];
210+
let cur = head;
211+
while (cur) {
212+
nums.push(cur.val);
213+
cur = cur.next;
214+
}
215+
216+
const n = nums.length;
217+
for (let i = 0, k = 1; i < n; i += k, k++) {
218+
// 最后一组, 可能出现不足
219+
k = Math.min(n - i, k);
220+
if (!(k & 1)) {
221+
let tmp = nums.splice(i, k);
222+
tmp.reverse();
223+
nums.splice(i, 0, ...tmp);
224+
}
225+
}
226+
227+
cur = head;
228+
for (let num of nums) {
229+
cur.val = num;
230+
cur = cur.next;
231+
}
232+
return head;
233+
};
234+
```
235+
193236
### **...**
194237

195238
```

‎solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README_EN.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,49 @@ class Solution {
174174
}
175175
```
176176

177+
### **TypeScript**
178+
179+
```ts
180+
/**
181+
* Definition for singly-linked list.
182+
* class ListNode {
183+
* val: number
184+
* next: ListNode | null
185+
* constructor(val?: number, next?: ListNode | null) {
186+
* this.val = (val===undefined ? 0 : val)
187+
* this.next = (next===undefined ? null : next)
188+
* }
189+
* }
190+
*/
191+
192+
function reverseEvenLengthGroups(head: ListNode | null): ListNode | null {
193+
let nums = [];
194+
let cur = head;
195+
while (cur) {
196+
nums.push(cur.val);
197+
cur = cur.next;
198+
}
199+
200+
const n = nums.length;
201+
for (let i = 0, k = 1; i < n; i += k, k++) {
202+
// 最后一组, 可能出现不足
203+
k = Math.min(n - i, k);
204+
if (!(k & 1)) {
205+
let tmp = nums.splice(i, k);
206+
tmp.reverse();
207+
nums.splice(i, 0, ...tmp);
208+
}
209+
}
210+
211+
cur = head;
212+
for (let num of nums) {
213+
cur.val = num;
214+
cur = cur.next;
215+
}
216+
return head;
217+
};
218+
```
219+
177220
### **...**
178221

179222
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 reverseEvenLengthGroups(head: ListNode | null): ListNode | null {
14+
let nums = [];
15+
let cur = head;
16+
while (cur) {
17+
nums.push(cur.val);
18+
cur = cur.next;
19+
}
20+
21+
const n = nums.length;
22+
for (let i = 0, k = 1; i < n; i += k, k++) {
23+
// 最后一组, 可能出现不足
24+
k = Math.min(n - i, k);
25+
if (!(k & 1)) {
26+
let tmp = nums.splice(i, k);
27+
tmp.reverse();
28+
nums.splice(i, 0, ...tmp);
29+
}
30+
}
31+
32+
cur = head;
33+
for (let num of nums) {
34+
cur.val = num;
35+
cur = cur.next;
36+
}
37+
return head;
38+
};

0 commit comments

Comments
(0)

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