146. LRU Cache

Practice LRU Cache for free on NudgeCode: Medium, Linked List pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Design a cache with a fixed capacity that evicts the least recently used key when it is full.

Both operations should run in O(1) average time.

Each test is a list of operations and their arguments; the expected output lists what each call returns (null for the constructor and for calls that return nothing).

Example

["LRUCache","put","put","get","put","get","put","get","get","get"]
[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]
->  [null,null,null,1,null,-1,null,-1,3,4]

Constraints