-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Delete the Middle Node of a Linked List
- Loading branch information
1 parent
747622f
commit fcc6af9
Showing
6 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
44 changes: 44 additions & 0 deletions
44
Medium/DeleteTheMiddleNodeOfALinkedList/include/solution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters