public void moveToTail(Node curt) { curt.prev = tail.prev; curt.next = tail; curt.prev.next = curt; curt.next.prev = curt; /*curt.prev = tail.prev; tail.prev = curt; curt.prev.next = curt; curt.next = tail;*/ }
public void set(int key, int value) { if (get(key) != -1) { map.get(key).val = value; return; } else { Node newNode = new Node(key, value); if (map.size() == capacity) { Node lruNode = head.next; map.remove(lruNode.key); head.next = head.next.next; head.next.prev = head; } map.put(key, newNode); moveToTail(newNode); } }
public LRUCache2(int capacity) { this.capacity = capacity; head.next = tail; tail.prev = head; }