Skip to content

Commit 674cce0

Browse files
authored
Update 2130-maximum-twin-sum-of-a-linked-list.js
1 parent 9610446 commit 674cce0

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

2130-maximum-twin-sum-of-a-linked-list.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {number}
11+
*/
12+
const pairSum = function(head) {
13+
let slow = head, fast = head
14+
while(fast && fast.next) {
15+
slow = slow.next
16+
fast = fast.next.next
17+
}
18+
// reverse
19+
let next = null, pre = null
20+
while(slow) {
21+
next = slow.next
22+
slow.next = pre
23+
pre = slow
24+
slow = next
25+
}
26+
27+
let res = 0
28+
while(pre) {
29+
res = Math.max(res, pre.val + head.val)
30+
pre = pre.next
31+
head = head.next
32+
}
33+
34+
return res
35+
};
36+
37+
// another
38+
139
/**
240
* Definition for singly-linked list.
341
* function ListNode(val, next) {

0 commit comments

Comments
 (0)