Tensorum
LeetCode 146 · Design

LRU Cache

LeetCode 146 · Medium · solution, intuition & step-by-step walkthrough

Hash map + doubly linked list with sentinels.

MediumHash TableLinked ListDesignDoubly-Linked List
149 msruntime · beats 5%
76.7 MBmemory · beats 7%
1approach
10accepted runs · last 10 Mar 2026

This is how I solved LRU Cache (LeetCode 146), a medium problem from the Design pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

You need O(1) lookup and O(1) 'move to most recent' / 'evict least recent'. A hash map finds nodes; a doubly linked list keeps recency order — the most recent sits after head, the least recent before tail.

Approach

  1. 1

    get: if present, move the node to the front and return its value.

  2. 2

    put: remove an existing node; if full, remove tail.prev.

  3. 3

    Insert the new node after head and record it in the map.

How I solved it

Hash map + doubly linked listPythonO(1) per op
class Node:
    def __init__(self,key,val):
        self.key = key
        self.val = val
        self.prev=None
        self.next=None

class LRUCache:
    def __init__(self, capacity: int):
        self.capacity = capacity
        self.head = Node(0,0)
        self.tail = Node(0,0)
        self.head.next = self.tail
        self.tail.prev = self.head
        self.loc_mpp = {}


    def get(self, key: int) -> int:
        if key in self.loc_mpp:
            node = self.loc_mpp.get(key)
            self.remove(node)
            self.insert(node)
            return node.val
        return -1

    def put(self, key: int, value: int) -> None:
        if key in self.loc_mpp:
            node = self.loc_mpp[key]
            self.remove(node)
        if len(self.loc_mpp)==self.capacity:
            self.remove(self.tail.prev)
        node = Node(key,value)
        self.insert(node)

    def insert(self,new_node) -> None:
        #head - next node
        #head - new_node -> next_node
        self.loc_mpp[new_node.key]=new_node
        next_node = self.head.next
        new_node.next = next_node
        next_node.prev = new_node
        self.head.next = new_node
        new_node.prev=self.head
    def remove(self,old_node) -> None:
        del self.loc_mpp[old_node.key]
        #prev_node old_node next_node
        prev_node = old_node.prev
        next_node = old_node.next
        prev_node.next = next_node
        next_node.prev = prev_node
        


# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
Loading the step-through…

LRU Cache — FAQ

What is the intuition behind LRU Cache?

You need O(1) lookup and O(1) 'move to most recent' / 'evict least recent'. A hash map finds nodes; a doubly linked list keeps recency order — the most recent sits after head, the least recent before tail.

How do you solve LRU Cache (LeetCode 146)?

1. get: if present, move the node to the front and return its value. 2. put: remove an existing node; if full, remove tail.prev. 3. Insert the new node after head and record it in the map.

What is the time and space complexity of LRU Cache?

Time: O(1) per operation. Space: O(capacity). (Accepted approach: Hash map + doubly linked list.)

What are common mistakes in LRU Cache?

Sentinel head and tail remove all null checks. Store the key in the node so eviction can delete it from the map.