Skip to content

Commit

Permalink
Added Delete the Middle Node of a Linked List
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace-Krypton committed Oct 3, 2023
1 parent 747622f commit fcc6af9
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Empty file.
Empty file.
44 changes: 44 additions & 0 deletions Medium/DeleteTheMiddleNodeOfALinkedList/include/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <vector>
#include <iostream>
#include <algorithm>
#include <gtest/gtest.h>

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;
}
}
};
19 changes: 19 additions & 0 deletions Medium/DeleteTheMiddleNodeOfALinkedList/tests/test.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit fcc6af9

Please sign in to comment.