Skip to content

Commit 9610446

Browse files
authored
Create 2130-maximum-twin-sum-of-a-linked-list.js
1 parent 6114e50 commit 9610446

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
const arr = []
14+
let cur = head
15+
16+
while(cur) {
17+
arr.push(cur.val)
18+
cur = cur.next
19+
}
20+
21+
let res = 0
22+
for(let i = 0, n = arr.length; i < n / 2; i++) {
23+
res = Math.max(res, arr[i] + arr[n - 1 - i])
24+
}
25+
26+
return res
27+
};

0 commit comments

Comments
 (0)