Skip to content

Commit 6e661e2

Browse files
Update LRU_Cache.java
1 parent fd09472 commit 6e661e2

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

LeetCode/LRU_Cache.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ private class ListNode {
4040
private ListNode head = null;
4141
private ListNode tail = null;
4242

43-
private void intialize(int capacity) {
43+
private void intialize(int capacity) { //this function will initialise the capacity
4444
this.capacity = capacity;
4545
this.size = 0;
4646
this.map = new HashMap<>();
47-
this.head = this.tail = null;
47+
this.head = this.tail = null; // this is the dummy node
4848
}
4949

50-
public LRUCache(int capacity) {
50+
public LRUCache(int capacity) {
5151
intialize(capacity);
5252
}
5353

54-
private void addLast(ListNode node) {
54+
private void addLast(ListNode node) { // in this function we will add every value of the node in the linkedlist
5555
if (this.head == null)
5656
this.head = this.tail = node;
5757
else {
@@ -63,7 +63,7 @@ private void addLast(ListNode node) {
6363

6464
}
6565

66-
private ListNode removeFirst() {
66+
private ListNode removeFirst() { // in this function the first node of the linkelist will be removed
6767
ListNode node = this.head;
6868
if (this.head == this.tail)
6969
this.head = this.tail = null;
@@ -75,7 +75,7 @@ private ListNode removeFirst() {
7575
return node;
7676
}
7777

78-
private ListNode removeLast() {
78+
private ListNode removeLast() { //remove last node from LinkedList
7979
ListNode node = this.tail;
8080
if (this.head == this.tail)
8181
this.head = this.tail = null;
@@ -87,7 +87,7 @@ private ListNode removeLast() {
8787
return node;
8888
}
8989

90-
private ListNode remove(ListNode node) {
90+
private ListNode remove(ListNode node) { //remove the node
9191
if (node == this.head)
9292
return removeFirst();
9393
else if (node == this.tail)
@@ -104,26 +104,26 @@ else if (node == this.tail)
104104
}
105105
}
106106

107-
private void makeRecent(ListNode node) {
107+
private void makeRecent(ListNode node) { // in this we move the node to the front of linkedlist
108108
if (node == this.tail)
109109
return;
110110

111111
remove(node);
112112
addLast(node);
113113
}
114114

115-
public int get(int key) {
116-
if (!map.containsKey(key))
115+
public int get(int key) { // get the value and add in the linkedlist
116+
if (!map.containsKey(key)) //if value is not present in the linked list then return -1
117117
return -1;
118118

119119
ListNode node = map.get(key);
120120
makeRecent(node);
121121
return node.value;
122122
}
123123

124-
public void put(int key, int value) {
125-
if (map.containsKey(key)) {
126-
ListNode node = map.get(key);
124+
public void put(int key, int value) { //in this function whenever we put a value first we will check that whether that value is present or not in an hashmap
125+
if (map.containsKey(key)) {
126+
ListNode node = map.get(key); // if value is present then we will update the value of the node
127127
node.value = value;
128128
makeRecent(node);
129129
} else {

0 commit comments

Comments
 (0)