File tree 2 files changed +18
-18
lines changed
2 files changed +18
-18
lines changed Original file line number Diff line number Diff line change 1
1
mod medium;
2
2
3
- use medium:: reorder_list:: ListNode ;
4
-
5
3
fn main ( ) {
6
- // Reorder List - Medium
7
- let mut head = Some ( Box :: new (
8
- ListNode :: new ( 1 ) . add_next (
9
- ListNode :: new ( 2 )
10
- . add_next ( ListNode :: new ( 3 ) . add_next ( ListNode :: new ( 4 ) . add_next ( ListNode :: new ( 5 ) ) ) ) ,
11
- ) ,
12
- ) ) ;
13
-
14
- medium:: reorder_list:: reorder_list ( & mut head) ;
15
-
16
- // Print elements of the list
17
- let mut curr = head;
18
- while let Some ( node) = curr {
19
- println ! ( "{}" , node. val) ;
20
- curr = node. next ;
21
- }
4
+ let mut cache = medium:: lru_cache:: LRUCache :: new ( 2 ) ;
5
+ cache. put ( 1 , 1 ) ;
6
+ cache. put ( 2 , 2 ) ;
7
+ println ! ( "{}" , cache. get( 1 ) ) ; // returns 1
8
+ cache. put ( 3 , 3 ) ; // evicts key 2
9
+ println ! ( "{}" , cache. get( 2 ) ) ; // returns -1 (not found)
10
+ cache. put ( 4 , 4 ) ; // evicts key 1
11
+ println ! ( "{}" , cache. get( 1 ) ) ; // returns -1 (not found)
12
+ println ! ( "{}" , cache. get( 3 ) ) ; // returns 3
13
+ println ! ( "{}" , cache. get( 4 ) ) ; // returns 4
22
14
}
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ impl ListNode {
23
23
}
24
24
}
25
25
26
+ /// A struct representing a doubly linked list node.
26
27
#[ derive( Debug ) ]
27
28
struct DoubleListNode {
28
29
head : Option < Rc < RefCell < ListNode > > > ,
@@ -202,6 +203,13 @@ impl LRUCache {
202
203
}
203
204
}
204
205
206
+ /*
207
+ Algorithm - Using a doubly linked list and a hashmap
208
+
209
+ Time O(1)
210
+ Space O(n)
211
+ */
212
+
205
213
#[ cfg( test) ]
206
214
mod tests {
207
215
use super :: * ;
You can’t perform that action at this time.
0 commit comments