From a8b42d0bb8f6a96af412d0bb3735ff6b15e92477 Mon Sep 17 00:00:00 2001 From: Sindhu Inti <89198083+Sindhuinti@users.noreply.github.com> Date: Sun, 9 Oct 2022 00:02:15 -0700 Subject: [PATCH] feat: leetcode Maximum Twin Sum of a Linked List solution (2130) (#988) --- leetcode/README.md | 1 + leetcode/src/2130.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 leetcode/src/2130.c diff --git a/leetcode/README.md b/leetcode/README.md index cdb81b46..b2003739 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -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| diff --git a/leetcode/src/2130.c b/leetcode/src/2130.c new file mode 100644 index 00000000..6dbacc1d --- /dev/null +++ b/leetcode/src/2130.c @@ -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; +}