@@ -286,6 +286,86 @@ var reorderList = function (head) {
286
286
};
287
287
```
288
288
289
+ ### ** TypeScript**
290
+
291
+ ``` ts
292
+ /**
293
+ * Definition for singly-linked list.
294
+ * class ListNode {
295
+ * val: number
296
+ * next: ListNode | null
297
+ * constructor(val?: number, next?: ListNode | null) {
298
+ * this.val = (val===undefined ? 0 : val)
299
+ * this.next = (next===undefined ? null : next)
300
+ * }
301
+ * }
302
+ */
303
+
304
+ /**
305
+ Do not return anything, modify head in-place instead.
306
+ */
307
+ function reorderList(head : ListNode | null ): void {
308
+ const arr = [];
309
+ let node = head ;
310
+ while (node .next != null ) {
311
+ arr .push (node );
312
+ node = node .next ;
313
+ }
314
+ let l = 0 ;
315
+ let r = arr .length - 1 ;
316
+ while (l < r ) {
317
+ const start = arr [l ];
318
+ const end = arr [r ];
319
+ [end .next .next , start .next , end .next ] = [start .next , end .next , null ];
320
+ l ++ ;
321
+ r -- ;
322
+ }
323
+ }
324
+ ```
325
+
326
+ ``` ts
327
+ /**
328
+ * Definition for singly-linked list.
329
+ * class ListNode {
330
+ * val: number
331
+ * next: ListNode | null
332
+ * constructor(val?: number, next?: ListNode | null) {
333
+ * this.val = (val===undefined ? 0 : val)
334
+ * this.next = (next===undefined ? null : next)
335
+ * }
336
+ * }
337
+ */
338
+
339
+ /**
340
+ Do not return anything, modify head in-place instead.
341
+ */
342
+ function reorderList(head : ListNode | null ): void {
343
+ let slow = head ;
344
+ let fast = head ;
345
+ // 找到中心节点
346
+ while (fast != null && fast .next != null ) {
347
+ slow = slow .next ;
348
+ fast = fast .next .next ;
349
+ }
350
+ // 反转节点
351
+ let next = slow .next ;
352
+ slow .next = null ;
353
+ while (next != null ) {
354
+ [next .next , slow , next ] = [slow , next , next .next ];
355
+ }
356
+ // 合并
357
+ let left = head ;
358
+ let right = slow ;
359
+ while (right .next != null ) {
360
+ const next = left .next ;
361
+ left .next = right ;
362
+ right = right .next ;
363
+ left .next .next = next ;
364
+ left = left .next .next ;
365
+ }
366
+ }
367
+ ```
368
+
289
369
### ** ...**
290
370
291
371
```
0 commit comments