diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b89f74..6c98ad0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1283,4 +1283,26 @@ add_executable(OddEvenLinkedList target_link_libraries( OddEvenLinkedList GTest::gtest_main +) + +# Reverse Linked List +add_executable(ReverseLinkedList + Easy/ReverseLinkedList/include/solution.hpp + Easy/ReverseLinkedList/tests/test.cpp +) + +target_link_libraries( + ReverseLinkedList + GTest::gtest_main +) + +# Maximum Twin Sum of a Linked List +add_executable(MaximumTwinSumOfALinkedList + Medium/MaximumTwinSumOfALinkedList/include/solution.hpp + Medium/MaximumTwinSumOfALinkedList/tests/test.cpp +) + +target_link_libraries( + MaximumTwinSumOfALinkedList + GTest::gtest_main ) \ No newline at end of file diff --git a/Easy/ReverseLinkedList/include/solution.hpp b/Easy/ReverseLinkedList/include/solution.hpp new file mode 100644 index 0000000..8ec9866 --- /dev/null +++ b/Easy/ReverseLinkedList/include/solution.hpp @@ -0,0 +1,45 @@ +#pragma once + +#include +#include +#include +#include + +struct ListNode { + int32_t value; + ListNode *next; +}; + +class Solution { +public: + static auto reverse_list(ListNode *head) -> ListNode* { + if (head == nullptr) return head; + + ListNode *current = head; + ListNode *prev = nullptr; + + while (current != nullptr) { + ListNode *next = current->next; + current->next = prev; + prev = current; + current = next; + } + + return prev; + } + + static auto insert(ListNode **head_ref, + const int32_t new_value) -> void { + auto *node = new ListNode(); + node->value = new_value; + node->next = (*head_ref); + (*head_ref) = node; + } + + static auto print(ListNode *head) -> void { + while (head != nullptr) { + std::cout << head->value << ' '; + head = head->next; + } + } +}; diff --git a/Easy/ReverseLinkedList/tests/test.cpp b/Easy/ReverseLinkedList/tests/test.cpp new file mode 100644 index 0000000..a14792c --- /dev/null +++ b/Easy/ReverseLinkedList/tests/test.cpp @@ -0,0 +1,17 @@ +#include "../include/solution.hpp" + +auto main() -> int { + ListNode *node = nullptr; + Solution::insert(&node, 5); + Solution::insert(&node, 4); + Solution::insert(&node, 3); + Solution::insert(&node, 2); + Solution::insert(&node, 1); + + Solution::print(node); + std::cout << '\n'; + node = Solution::reverse_list(node); + Solution::print(node); + + return 0; +} diff --git a/Medium/MaximumTwinSumOfALinkedList/include/solution.hpp b/Medium/MaximumTwinSumOfALinkedList/include/solution.hpp new file mode 100644 index 0000000..228f1aa --- /dev/null +++ b/Medium/MaximumTwinSumOfALinkedList/include/solution.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include +#include + +struct ListNode { + int32_t value; + ListNode *next; +}; + +class Solution { +public: + static auto pair_sum(ListNode *head) -> int32_t { + + } + + static auto insert(ListNode **head_ref) +}; diff --git a/Medium/MaximumTwinSumOfALinkedList/tests/test.cpp b/Medium/MaximumTwinSumOfALinkedList/tests/test.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Medium/ReverseLinkedListII/include/solution.hpp b/Medium/ReverseLinkedListII/include/solution.hpp index 9300807..a38b3ca 100644 --- a/Medium/ReverseLinkedListII/include/solution.hpp +++ b/Medium/ReverseLinkedListII/include/solution.hpp @@ -21,7 +21,7 @@ class Solution { ListNode *prev = dummy; ListNode *curr = head; - for (std::size_t i = 1; i < left; ++i) { + for (size_t i = 1; i < left; ++i) { prev = curr; curr = curr->next; }