Skip to content

Commit b0cfbaa

Browse files
authored
Fix 146-Lru-Cache.cpp removed nodes deallocation
Nodes removed from the List and the HashMap aren't deallocated
1 parent 43119ae commit b0cfbaa

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

cpp/146-Lru-Cache.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class LRUCache {
4747
void put(int key, int value) {
4848
if (cache.find(key) != cache.end()) {
4949
remove(cache[key]);
50+
51+
// Free allocated memory for the removed node
52+
delete cache[key];
5053
}
5154
cache[key] = new Node(key, value);
5255
insert(cache[key]);
@@ -56,6 +59,9 @@ class LRUCache {
5659
Node* lru = left->next;
5760
remove(lru);
5861
cache.erase(lru->k);
62+
63+
// Free allocated memory for the removed node
64+
delete lru;
5965
}
6066
}
6167
private:

0 commit comments

Comments
 (0)