Skip to content

Commit 6132cf2

Browse files
committed
Remove node explicitly before adding to head instead of push_head moving it
1 parent 3fd9e83 commit 6132cf2

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Sprint-2/implement_lru_cache/linked_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def push_head(self,node=None,key=None,value=None) -> Node:
3030
if node is None:
3131
new_node=Node(key,value)
3232
else:
33-
self.remove(node)
33+
# self.remove(node)
3434
new_node=node
3535

3636
if self.head is None:

Sprint-2/implement_lru_cache/lru_cache.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def get(self,key) ->any:
2121
"""
2222
if key not in self.map:
2323
return None
24-
node=self.map[key]
24+
node=self.map[key]
25+
self.list.remove(node)
2526
self.list.push_head(node=node)
2627
return node.value
2728

@@ -39,7 +40,8 @@ def set(self,key,value):
3940
"""
4041
if key in self.map:
4142
node=self.map[key]
42-
node.value=value
43+
node.value=value
44+
self.list.remove(node)
4345
self.list.push_head(node=node)
4446
else:
4547
if self.count>=self.limit:

0 commit comments

Comments
 (0)