mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
feat: leetcode Maximum Twin Sum of a Linked List solution (2130) (#988)
This commit is contained in:
parent
82ca460a9c
commit
a8b42d0bb8
@ -93,3 +93,4 @@ LeetCode
|
|||||||
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c)|Easy|
|
|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|
|
|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|
|
|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
30
leetcode/src/2130.c
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user