File tree 3 files changed +124
-0
lines changed
solution/2000-2099/2074.Reverse Nodes in Even Length Groups
3 files changed +124
-0
lines changed Original file line number Diff line number Diff line change @@ -190,6 +190,49 @@ class Solution {
190
190
}
191
191
```
192
192
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
+
193
236
### ** ...**
194
237
195
238
```
Original file line number Diff line number Diff line change @@ -174,6 +174,49 @@ class Solution {
174
174
}
175
175
```
176
176
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
+
177
220
### ** ...**
178
221
179
222
```
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments