diff --git a/apps/docs/content/(hash-table)/146-lru-cache.mdx b/apps/docs/content/(hash-table)/146-lru-cache.mdx index 2bca646..60b72e0 100644 --- a/apps/docs/content/(hash-table)/146-lru-cache.mdx +++ b/apps/docs/content/(hash-table)/146-lru-cache.mdx @@ -47,10 +47,11 @@ class LRUCache: next_node.prev = prev_node def _add_node(self, node): - node.prev = self.head - node.next = self.head.next - self.head.next.prev = node # pyright: ignore[reportAttributeAccessIssue] - self.head.next = node + # Insert just before the tail (most recent at end) + node.next = self.tail + node.prev = self.tail.prev + self.tail.prev.next = node # pyright: ignore[reportAttributeAccessIssue] + self.tail.prev = node def get(self, key: int) -> int: if key not in self.cache: @@ -73,7 +74,7 @@ class LRUCache: self._add_node(new_node) if len(self.cache) > self.capacity: - lru = self.tail.prev + lru = self.head.next # Oldest node is next to the head self._remove_node(lru) del self.cache[lru.key] # pyright: ignore[reportAttributeAccessIssue]