mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 18:46:50 +08:00
fix: memory leak in median_search2.cpp (#2723)
This commit is contained in:
@@ -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";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user