From fcc6af9cd6fca4b157779d1cceb03fb3fc7e5e5e Mon Sep 17 00:00:00 2001 From: Ace-Krypton Date: Tue, 3 Oct 2023 18:41:33 +0200 Subject: [PATCH] Added Delete the Middle Node of a Linked List --- CMakeLists.txt | 22 ++++++++++ Easy/NumberOfGoodPairs/include/solution.hpp | 0 Easy/NumberOfGoodPairs/tests/test.cpp | 0 .../include/solution.hpp | 44 +++++++++++++++++++ .../tests/test.cpp | 19 ++++++++ .../include/solution.hpp | 3 +- 6 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 Easy/NumberOfGoodPairs/include/solution.hpp create mode 100644 Easy/NumberOfGoodPairs/tests/test.cpp create mode 100644 Medium/DeleteTheMiddleNodeOfALinkedList/include/solution.hpp create mode 100644 Medium/DeleteTheMiddleNodeOfALinkedList/tests/test.cpp 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]