structNode{intkey;intvalue;Node(intkey,intvalue):key(key),value(value){}};classLRUCache{public:LRUCache(intcapacity):capacity(capacity){}intget(intkey){if(!keyToIterator.count(key))return-1;constauto&it=keyToIterator[key];// Move it to the frontcache.splice(begin(cache),cache,it);returnit->value;}voidput(intkey,intvalue){// No capacity issue, just update the valueif(keyToIterator.count(key)){constauto&it=keyToIterator[key];// Move it to the frontcache.splice(begin(cache),cache,it);it->value=value;return;}// Check the capacityif(cache.size()==capacity){constNode&lastNode=cache.back();// that's why we store `key` in `Node`keyToIterator.erase(lastNode.key);cache.pop_back();}cache.emplace_front(key,value);keyToIterator[key]=begin(cache);}private:constintcapacity;list<Node>cache;unordered_map<int,list<Node>::iterator>keyToIterator;};