Skip to content

Commit

Permalink
fix: memory leak in median_search2.cpp (#2723)
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 authored Sep 4, 2024
1 parent c652c4f commit 490974e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions search/median_search2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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";
}

Expand Down

0 comments on commit 490974e

Please sign in to comment.