Design Least Recently Used Cache (LRU) using Doubly Linked List
Implement a doubly linked list. Each node in the doubly linked list must have three attributes:
value
– the value of the current nodenext
– a pointer/reference to the next nodeprev
– a pointer/reference to the previous nodeAssume linked list is 0-index based.
Implement the CustomLinkedList
class with the following methods:
CustomLinkedList()
– Initializes the CustomLinkedList
object.int get(int index)
– Get the value of the index
th node in the linked list. If the index is invalid, return -1.void addAtHead(int value)
– Add a node of value value
before the first element of the linked list. After insertion, the new node will be the first node.void addAtTail(int value)
– Append a node of value value
as the last element of the linked list.void addAtIndex(int index, int value)
– Add a node of value value
before the index
th node. If index
equals the length, the node will be appended to the end. If index
is greater than the length, the node will not be inserted.void deleteAtIndex(int index)
– Delete the index
th node, if the index is valid.LinkedList
library.get
, addAtHead
, addAtTail
, addAtIndex
, and deleteAtIndex
.