I'm looking at Leetcode's solution for 146 LRU Caches. I have a question about Node(). Essentially, the question asks us to: Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key. The functions get and put must each run in O(1) average time complexity.
Essentially, what's in the code right now is
class Node:
def _init_(self, key, val):
self.key, self.val = key, val
self.prev = self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.cache = {}
self.cap = capacity
self.left, self.right = Node(0,0), Node(0,0)
self.left.next, self.right.prev = self.right, self.left
I'm confused about this part specifically. I get the error message that Node doesn't take in any arguments. How is that so? Doesn't the initializer have a key and a value argument being put in?