Let's implement get
! All get
needs to do is find a key
in this.cache
. If found, we moveToHead
to let keep it as the most recently used key, and return it. Otherwise, we return -1
.
1get(key) {
2 const node = this.cache[key];
3 if (!node) {
4 return -1;
5 }
6 this.moveToHead(node);
7 return node.val;
8};