aboutsummaryrefslogtreecommitdiff
path: root/max_twin_sum_list.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /max_twin_sum_list.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'max_twin_sum_list.cpp')
-rw-r--r--max_twin_sum_list.cpp41
1 files changed, 0 insertions, 41 deletions
diff --git a/max_twin_sum_list.cpp b/max_twin_sum_list.cpp
deleted file mode 100644
index dd635e1..0000000
--- a/max_twin_sum_list.cpp
+++ /dev/null
@@ -1,41 +0,0 @@
-// 2130. Maximum Twin Sum of a Linked List
-#include "leetcode.h"
-
-/*
- * in a linked-list of size 'n', where 'n' is even, the i'th node (0-indexed) of
- * the linked list is known as the twin of the 'n - 1 - i' node, if 0 <= i <= (n
- * / 2) - 1. for example, if 'n = 4', then node 0 is the twin of node 3, and
- * node 1 is the twin node of node 2. these are the only nodes with twins for 'n
- * = 4'. the twin sum is defined as the sum of a node and its twin. given the
- * 'head' of a linked list with even length, return the maximum twin sum of the
- * linked list.
- *
- */
-
-class Solution {
-public:
- int pairSum(ListNode *head) {
- int ans = 0;
- auto slow = head, fast = head;
- while (fast != nullptr && fast->next != nullptr) {
- fast = fast->next->next;
- slow = slow->next;
- }
- slow = reverseList(slow);
- while (slow != nullptr) {
- ans = max(ans, slow->val + head->val);
- slow = slow->next;
- head = head->next;
- }
- return ans;
- }
-
-private:
- ListNode *reverseList(ListNode *curr, ListNode *prev = nullptr) {
- while (curr != nullptr) {
- swap(curr->next, prev);
- swap(prev != nullptr ? prev->next : prev, curr);
- }
- return prev;
- }
-};