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;
}
profile-image
Hi, 我是 Zeki。目前為一名前端工程師。我相信科技始終來自於人性,是用來幫助人們過上更有品質的生活的,但願也希望如此。