程式語言 - LeetCode - C - 2130. Maximum Twin Sum of a Linked List



題目:


解答:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
int pairSum(struct ListNode *head)
{
    int r = 0;
    int c0 = 0;
    int cnt = 0;
    int val[100000] = { 0 };

    if (head == NULL) {
        return 0;
    }

    while (head) {
        val[cnt++] = head->val;
        head = head->next;
    }

    for (c0 = 0; c0 < (cnt >> 1); c0++) {
        int t = val[c0] + val[cnt - c0 - 1];

        if (t > r) {
            r = t;
        }
    }

    return r;
}