2130. 链表最大孪生和
题目链接:2130. 链表最大孪生和
代码如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int pairSum(ListNode* head)
{
vector<int> arr;
int res = 0;
for(ListNode* pHead = head;pHead;pHead=pHead->next) {arr.push_back(pHead->val);}
for(int i=arr.size()/2-1;i>=0;i--)
{
int sum=arr[i]+arr[arr.size()-i-1];
res=max(res,sum);
}
return res;
}
};