diff --git a/CMakeLists.txt b/CMakeLists.txt index 20f33f5..34f4cf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1250,4 +1250,26 @@ add_executable(RemoveColoredPiecesIfBothNeighborsAreTheSameColor target_link_libraries( RemoveColoredPiecesIfBothNeighborsAreTheSameColor GTest::gtest_main +) + +# Delete the Middle Node of a Linked List +add_executable(DeleteTheMiddleNodeOfALinkedList + Medium/DeleteTheMiddleNodeOfALinkedList/include/solution.hpp + Medium/DeleteTheMiddleNodeOfALinkedList/tests/test.cpp +) + +target_link_libraries( + DeleteTheMiddleNodeOfALinkedList + GTest::gtest_main +) + +# Number of Good Pairs +add_executable(NumberOfGoodPairs + Easy/NumberOfGoodPairs/include/solution.hpp + Easy/NumberOfGoodPairs/tests/test.cpp +) + +target_link_libraries( + NumberOfGoodPairs + GTest::gtest_main ) \ No newline at end of file diff --git a/Easy/NumberOfGoodPairs/include/solution.hpp b/Easy/NumberOfGoodPairs/include/solution.hpp new file mode 100644 index 0000000..e69de29 diff --git a/Easy/NumberOfGoodPairs/tests/test.cpp b/Easy/NumberOfGoodPairs/tests/test.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Medium/DeleteTheMiddleNodeOfALinkedList/include/solution.hpp b/Medium/DeleteTheMiddleNodeOfALinkedList/include/solution.hpp new file mode 100644 index 0000000..82c749e --- /dev/null +++ b/Medium/DeleteTheMiddleNodeOfALinkedList/include/solution.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include + +struct ListNode { + int32_t value; + ListNode *next; +}; + +class Solution { +public: + static auto delete_middle(ListNode *head) -> ListNode* { + if (head->next == nullptr) return nullptr; + + ListNode *slow = head; + ListNode *fast = head->next->next; + + while (fast != nullptr && fast->next != nullptr) { + slow = slow->next; + fast = fast->next->next; + } + + slow->next = slow->next->next; + return head; + } + + 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->next != nullptr) { + std::cout << head->value << ' '; + head = head->next; + } + } +}; diff --git a/Medium/DeleteTheMiddleNodeOfALinkedList/tests/test.cpp b/Medium/DeleteTheMiddleNodeOfALinkedList/tests/test.cpp new file mode 100644 index 0000000..41a45a6 --- /dev/null +++ b/Medium/DeleteTheMiddleNodeOfALinkedList/tests/test.cpp @@ -0,0 +1,19 @@ +#include "../include/solution.hpp" + +auto main() -> int { + ListNode *node = nullptr; + Solution::insert(&node, 1); + Solution::insert(&node, 3); + Solution::insert(&node, 4); + Solution::insert(&node, 7); + Solution::insert(&node, 1); + Solution::insert(&node, 2); + Solution::insert(&node, 6); + + Solution::print(node); + std::cout << '\n'; + node = Solution::delete_middle(node); + Solution::print(node); + + return 0; +} diff --git a/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp b/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp index ece7f6f..403dd55 100644 --- a/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp +++ b/Medium/RemoveColoredPiecesIfBothNeighborsAreTheSameColor/include/solution.hpp @@ -8,8 +8,7 @@ class Solution { public: static auto winner_of_game(const std::string &colors) -> bool { - int32_t bob = 0; - int32_t alice = 0; + int32_t bob = 0, alice = 0; for (size_t i = 1; i < colors.size() - 1; ++i) { if (colors[i - 1] == colors[i]