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



題目:


解答:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * 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;
}