If we are implementing a LRU
cache using HashMap
and DoublyLinkedList
, What is the best way to implement evict()
method with O(1)
time complexity?
LinkedList
from Java
didn't expose the Node
type (which is a private static
inner class).
So you can't remove it in in O(1)
, because a sequential scan is required.
To get O(1)
, you need to be able to access the Node
type, so that could remove it without scan.
You have to write it by yourself. Fortunately, a doubly linked list
is relatively easy to write, and it's a pretty beneficial & fun task to do.
Node
?Refer to this answer: https://stackoverflow.com/a/54593530
The method LinkedList.java
-> removeNode()
remove a given node, without sequential scan.
The code in this answer is for a singly linked list, the remove
for a doubly linked list is even simpler in some case.
Tips:
singly linked list
, for a doubly linked node
, the node itself contains the previous node, so you don't have to pass previous node to the removeNode()
method.BTW
linked list
is the most basic structure (except array
and bits
), that some other very basic structures could built base on.queue
and stack
could be implemented easily with a linked list
.java.util.LinkedList
is not thread-safe, your LRU might needs some concurrent control, but I'm not sure.java.util.concurrent.ConcurrentLinkedDeque
is a good example to refer to.LinkedHashMap
java.util.LinkedHashMap
, is a combination of hashtable & doubly linked list.
Mechanism:
HashMap
to get the O(1)
complexity for the common operations.doubly linked list
to keep track of insertion order.head
is the eldest item, and tail
is the newest item.It can be used to impl some kind of cache, though I am not sure will it be fully qualified for your requirement.