From 038fa42883f3d943ef2aa1aa14d236acb731198b Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Mon, 30 Jan 2023 20:17:47 +0800 Subject: [PATCH] Refactor structure --- include/s0002_add_two_numbers.hpp | 8 +------ ...s0019_remove_nth_node_from_end_of_list.hpp | 8 +------ include/s0021_merge_two_sorted_lists.hpp | 10 ++------- include/s0023_merge_k_sorted_lists.hpp | 10 ++------- include/s0024_swap_nodes_in_pairs.hpp | 8 +------ include/s0025_reverse_nodes_in_k-group.hpp | 10 ++------- include/s0142_linked_list_cycle.hpp | 8 ++----- ...s0160_intersection_of_two_linked_lists.hpp | 6 +---- include/s0203_remove_linked_list_elements.hpp | 8 +------ include/s0206_reverse_linked_list.hpp | 8 +------ include/s0226_invert_binary_tree.hpp | 11 ++++++++++ include/structures.hpp | 22 +++++++++++++++++++ 12 files changed, 47 insertions(+), 70 deletions(-) create mode 100644 include/s0226_invert_binary_tree.hpp create mode 100644 include/structures.hpp diff --git a/include/s0002_add_two_numbers.hpp b/include/s0002_add_two_numbers.hpp index c6d0899..29ca098 100644 --- a/include/s0002_add_two_numbers.hpp +++ b/include/s0002_add_two_numbers.hpp @@ -1,13 +1,7 @@ #ifndef S0002_ADD_TWO_NUMBERS_HPP #define S0002_ADD_TWO_NUMBERS_HPP -struct ListNode { - int val; - ListNode* next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode* next) : val(x), next(next) {} -}; +#include "structures.hpp" class S0002 { public: diff --git a/include/s0019_remove_nth_node_from_end_of_list.hpp b/include/s0019_remove_nth_node_from_end_of_list.hpp index 204e2a2..acab45c 100644 --- a/include/s0019_remove_nth_node_from_end_of_list.hpp +++ b/include/s0019_remove_nth_node_from_end_of_list.hpp @@ -1,13 +1,7 @@ #ifndef S0019_REMOVE_NTH_NODE_FROM_END_OF_LIST_HPP #define S0019_REMOVE_NTH_NODE_FROM_END_OF_LIST_HPP -struct ListNode { - int val; - ListNode* next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode* next) : val(x), next(next) {} -}; +#include "structures.hpp" class S0019 { public: diff --git a/include/s0021_merge_two_sorted_lists.hpp b/include/s0021_merge_two_sorted_lists.hpp index 809025a..a719a83 100644 --- a/include/s0021_merge_two_sorted_lists.hpp +++ b/include/s0021_merge_two_sorted_lists.hpp @@ -3,15 +3,9 @@ #include -using namespace std; +#include "structures.hpp" -struct ListNode { - int val; - ListNode* next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode* next) : val(x), next(next) {} -}; +using namespace std; class S0021 { public: diff --git a/include/s0023_merge_k_sorted_lists.hpp b/include/s0023_merge_k_sorted_lists.hpp index 8687c49..2e4bf3c 100644 --- a/include/s0023_merge_k_sorted_lists.hpp +++ b/include/s0023_merge_k_sorted_lists.hpp @@ -3,15 +3,9 @@ #include -using namespace std; +#include "structures.hpp" -struct ListNode { - int val; - ListNode *next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode *next) : val(x), next(next) {} -}; +using namespace std; class S0023 { public: diff --git a/include/s0024_swap_nodes_in_pairs.hpp b/include/s0024_swap_nodes_in_pairs.hpp index eeba727..f2c6ad8 100644 --- a/include/s0024_swap_nodes_in_pairs.hpp +++ b/include/s0024_swap_nodes_in_pairs.hpp @@ -1,13 +1,7 @@ #ifndef S0024_SWAP_NODES_IN_PAIRS_HPP #define S0024_SWAP_NODES_IN_PAIRS_HPP -struct ListNode { - int val; - ListNode* next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode* next) : val(x), next(next) {} -}; +#include "structures.hpp" class S0024 { public: diff --git a/include/s0025_reverse_nodes_in_k-group.hpp b/include/s0025_reverse_nodes_in_k-group.hpp index da55d74..d162d2f 100644 --- a/include/s0025_reverse_nodes_in_k-group.hpp +++ b/include/s0025_reverse_nodes_in_k-group.hpp @@ -4,15 +4,9 @@ #include #include -using namespace std; +#include "structures.hpp" -struct ListNode { - int val; - ListNode* next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode* next) : val(x), next(next) {} -}; +using namespace std; class S0025 { public: diff --git a/include/s0142_linked_list_cycle.hpp b/include/s0142_linked_list_cycle.hpp index d80835d..b7b9a4b 100644 --- a/include/s0142_linked_list_cycle.hpp +++ b/include/s0142_linked_list_cycle.hpp @@ -3,11 +3,7 @@ #include -struct ListNode { - int val; - ListNode* next; - ListNode(int x) : val(x), next(nullptr) {} -}; +#include "structures.hpp" struct IterResult { ListNode *node; @@ -17,7 +13,7 @@ struct IterResult { class S0142 { public: std::unordered_map footprint; - IterResult iter(ListNode *fast, ListNode* slow, bool startUp); + IterResult iter(ListNode *fast, ListNode *slow, bool startUp); ListNode *detectCycle(ListNode *head); }; diff --git a/include/s0160_intersection_of_two_linked_lists.hpp b/include/s0160_intersection_of_two_linked_lists.hpp index 3ce45b1..c4d25d7 100644 --- a/include/s0160_intersection_of_two_linked_lists.hpp +++ b/include/s0160_intersection_of_two_linked_lists.hpp @@ -1,11 +1,7 @@ #ifndef S0160_INTERSECTION_OF_TWO_LINKED_LISTS_HPP #define S0160_INTERSECTION_OF_TWO_LINKED_LISTS_HPP -struct ListNode { - int val; - ListNode *next; - ListNode(int x) : val(x), next(nullptr) {} -}; +#include "structures.hpp" class S0160 { public: diff --git a/include/s0203_remove_linked_list_elements.hpp b/include/s0203_remove_linked_list_elements.hpp index b72e06b..a7e39fc 100644 --- a/include/s0203_remove_linked_list_elements.hpp +++ b/include/s0203_remove_linked_list_elements.hpp @@ -1,13 +1,7 @@ #ifndef S0203_REMOVE_LINKED_LIST_ELEMENTS_HPP #define S0203_REMOVE_LINKED_LIST_ELEMENTS_HPP -struct ListNode { - int val; - ListNode* next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode* next) : val(x), next(next) {} -}; +#include "structures.hpp" class S0203 { public: diff --git a/include/s0206_reverse_linked_list.hpp b/include/s0206_reverse_linked_list.hpp index 664ed85..f85fe2b 100644 --- a/include/s0206_reverse_linked_list.hpp +++ b/include/s0206_reverse_linked_list.hpp @@ -1,13 +1,7 @@ #ifndef S0206_REVERSE_LINKED_LIST_HPP #define S0206_REVERSE_LINKED_LIST_HPP -struct ListNode { - int val; - ListNode* next; - ListNode() : val(0), next(nullptr) {} - ListNode(int x) : val(x), next(nullptr) {} - ListNode(int x, ListNode* next) : val(x), next(next) {} -}; +#include "structures.hpp" class S0206 { public: diff --git a/include/s0226_invert_binary_tree.hpp b/include/s0226_invert_binary_tree.hpp new file mode 100644 index 0000000..3c25e4c --- /dev/null +++ b/include/s0226_invert_binary_tree.hpp @@ -0,0 +1,11 @@ +#ifndef S0226_INVERT_BINARY_TREE_HPP +#define S0226_INVERT_BINARY_TREE_HPP + +#include "structures.hpp" + +class S0226 { + public: + TreeNode* invertTree(TreeNode* root); +}; + +#endif diff --git a/include/structures.hpp b/include/structures.hpp new file mode 100644 index 0000000..994a957 --- /dev/null +++ b/include/structures.hpp @@ -0,0 +1,22 @@ +#ifndef STRUCTURES_HPP +#define STRUCTURES_HPP + +struct ListNode { + int val; + ListNode *next; + ListNode() : val(0), next(nullptr) {} + ListNode(int x) : val(x), next(nullptr) {} + ListNode(int x, ListNode *next) : val(x), next(next) {} +}; + +struct TreeNode { + int val; + TreeNode *left; + TreeNode *right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode *left, TreeNode *right) + : val(x), left(left), right(right) {} +}; + +#endif