File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -144,15 +144,15 @@ impl<T> LinkedList<T> {
144144 }
145145
146146 pub fn delete_ith ( & mut self , index : u32 ) -> Option < T > {
147- if self . length < index {
147+ if self . length <= index {
148148 panic ! ( "Index out of bounds" ) ;
149149 }
150150
151151 if index == 0 || self . head . is_none ( ) {
152152 return self . delete_head ( ) ;
153153 }
154154
155- if self . length == index {
155+ if self . length - 1 == index {
156156 return self . delete_tail ( ) ;
157157 }
158158
@@ -499,4 +499,14 @@ mod tests {
499499 assert ! ( retrived_item. is_some( ) ) ;
500500 assert_eq ! ( "B" , * retrived_item. unwrap( ) ) ;
501501 }
502+
503+ #[ test]
504+ #[ should_panic( expected = "Index out of bounds" ) ]
505+ fn delete_ith_panics_if_index_equals_length ( ) {
506+ let mut list = LinkedList :: < i32 > :: new ( ) ;
507+ list. insert_at_tail ( 1 ) ;
508+ list. insert_at_tail ( 2 ) ;
509+ // length is 2, so index 2 is out of bounds
510+ list. delete_ith ( 2 ) ;
511+ }
502512}
You can’t perform that action at this time.
0 commit comments