feat: leetcode Maximum Twin Sum of a Linked List solution (2130) (#988)

This commit is contained in:
Sindhu Inti 2022-10-09 00:02:15 -07:00 committed by GitHub
parent 82ca460a9c
commit a8b42d0bb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -93,3 +93,4 @@ LeetCode
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c)|Easy|
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy|
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c)|Easy|
|2130|[Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c)|Medium|

30
leetcode/src/2130.c Normal file
View File

@ -0,0 +1,30 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int pairSum(struct ListNode* head)
{
struct ListNode* dup = head;
int count = 0, i = 0, max = 0;
while (head != NULL)
{
count++;
head = head->next;
}
int* arr = malloc(count * sizeof(int));
while (dup != NULL)
{
arr[i++] = dup->val;
dup = dup->next;
}
for (i = 0; i < count / 2; ++i)
{
if (arr[i] + arr[count - i - 1] > max)
max = arr[i] + arr[count - i - 1];
}
return max;
}