diff --git a/search/median_search2.cpp b/search/median_search2.cpp index 2e9b04f9f10..bec6959d838 100644 --- a/search/median_search2.cpp +++ b/search/median_search2.cpp @@ -73,6 +73,13 @@ ListNode* middleNode(ListNode* head) { return (fastptr->next) ? slowptr->next : slowptr; } + +void deleteAll(const ListNode* const head) { + if (head) { + deleteAll(head->next); + delete head; + } +} } // namespace median_search2 } // namespace search @@ -98,6 +105,7 @@ static void test() { ListNode* median = search::median_search2::middleNode(head1); assert(3 == median->val); // 3 is the value of the median node. + search::median_search2::deleteAll(head1); std::cout << "test case:1 passed\n"; // Test case # 2 @@ -118,14 +126,9 @@ static void test() { ListNode* median1 = search::median_search2::middleNode(head2); assert(4 == median1->val); // 4 is the value of the median node. + search::median_search2::deleteAll(head2); std::cout << "test case:2 passed\n"; - delete head1; - delete temp; - - delete head2; - delete temp2; - std::cout << "--All tests passed--\n"; }