Maximum Twin Sum of a Linked List
LeetCode題目: 2130. Maximum Twin Sum of a Linked List
My solution:
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {number}
*/
let pairSum = head => {
let max = 0;
let node = null;
let slow = head;
let fast = head;
while(fast !== null && fast.next !== null) {
fast = fast.next.next;
const temp = slow.next;
slow.next = node;
node = slow;
slow = temp
}
while(slow !== null) {
max = Math.max(max, slow.val + node.val);
slow = slow.next;
node = node.next;
}
return max;
}