@@ -352,10 +352,110 @@ func max(a, b int) int {
352
352
353
353
### ** TypeScript**
354
354
355
- <!-- 这里可写当前语言的特殊实现逻辑 -->
355
+ ``` ts
356
+ /**
357
+ * Definition for singly-linked list.
358
+ * class ListNode {
359
+ * val: number
360
+ * next: ListNode | null
361
+ * constructor(val?: number, next?: ListNode | null) {
362
+ * this.val = (val===undefined ? 0 : val)
363
+ * this.next = (next===undefined ? null : next)
364
+ * }
365
+ * }
366
+ */
367
+
368
+ function pairSum(head : ListNode | null ): number {
369
+ const arr = [];
370
+ let node = head ;
371
+ while (node ) {
372
+ arr .push (node .val );
373
+ node = node .next ;
374
+ }
375
+ const n = arr .length ;
376
+ let ans = 0 ;
377
+ for (let i = 0 ; i < n >> 1 ; i ++ ) {
378
+ ans = Math .max (ans , arr [i ] + arr [n - 1 - i ]);
379
+ }
380
+ return ans ;
381
+ }
382
+ ```
356
383
357
384
``` ts
385
+ /**
386
+ * Definition for singly-linked list.
387
+ * class ListNode {
388
+ * val: number
389
+ * next: ListNode | null
390
+ * constructor(val?: number, next?: ListNode | null) {
391
+ * this.val = (val===undefined ? 0 : val)
392
+ * this.next = (next===undefined ? null : next)
393
+ * }
394
+ * }
395
+ */
396
+
397
+ function pairSum(head : ListNode | null ): number {
398
+ let fast = head ;
399
+ let slow = head ;
400
+ while (fast ) {
401
+ fast = fast .next .next ;
402
+ slow = slow .next ;
403
+ }
404
+ let prev = null ;
405
+ while (slow ) {
406
+ const next = slow .next ;
407
+ slow .next = prev ;
408
+ prev = slow ;
409
+ slow = next ;
410
+ }
411
+ let left = head ;
412
+ let right = prev ;
413
+ let ans = 0 ;
414
+ while (left && right ) {
415
+ ans = Math .max (ans , left .val + right .val );
416
+ left = left .next ;
417
+ right = right .next ;
418
+ }
419
+ return ans ;
420
+ }
421
+ ```
358
422
423
+ ### ** Rust**
424
+
425
+ ``` rust
426
+ // Definition for singly-linked list.
427
+ // #[derive(PartialEq, Eq, Clone, Debug)]
428
+ // pub struct ListNode {
429
+ // pub val: i32,
430
+ // pub next: Option<Box<ListNode>>
431
+ // }
432
+ //
433
+ // impl ListNode {
434
+ // #[inline]
435
+ // fn new(val: i32) -> Self {
436
+ // ListNode {
437
+ // next: None,
438
+ // val
439
+ // }
440
+ // }
441
+ // }
442
+ impl Solution {
443
+ pub fn pair_sum (head : Option <Box <ListNode >>) -> i32 {
444
+ let mut arr = Vec :: new ();
445
+ let mut node = & head ;
446
+ while node . is_some () {
447
+ let t = node . as_ref (). unwrap ();
448
+ arr . push (t . val);
449
+ node = & t . next;
450
+ }
451
+ let n = arr . len ();
452
+ let mut ans = 0 ;
453
+ for i in 0 .. n >> 1 {
454
+ ans = ans . max (arr [i ] + arr [n - 1 - i ]);
455
+ }
456
+ ans
457
+ }
458
+ }
359
459
```
360
460
361
461
### ** ...**
0 commit comments