1
1
#![ forbid( unsafe_code) ]
2
+ use minivec:: MiniVec ;
2
3
8000
use std:: cell:: { Ref , RefCell , RefMut } ;
3
4
use std:: cmp:: Ordering ;
4
5
use std:: fmt:: { Debug , Error , Formatter } ;
5
6
use std:: ops:: { Add , Deref , DerefMut } ;
6
7
use std:: rc:: Rc ;
7
- use minivec:: MiniVec ;
8
8
9
9
type Cell < T > = Rc < RefCell < Node < T > > > ;
10
10
type Link < T > = Option < Cell < T > > ;
@@ -285,12 +285,7 @@ impl<T: Default> LinkedList<T> {
285
285
result
286
286
}
287
287
288
- fn partition_at_tail (
289
- & self ,
290
- high : usize ,
291
- ascending : bool ,
292
- cells : & MiniVec < Cell < T > > ,
293
- ) -> usize
288
+ fn partition_at_tail ( & self , high : usize , ascending : bool , cells : & MiniVec < Cell < T > > ) -> usize
294
289
where
295
290
T : PartialOrd ,
296
291
{
@@ -307,7 +302,10 @@ impl<T: Default> LinkedList<T> {
307
302
} ;
308
303
if less_or_eq_or_greater_or_eq {
309
304
if i != j {
310
- std:: mem:: swap ( & mut cells[ i] . borrow_mut ( ) . elem , & mut cells[ j] . borrow_mut ( ) . elem ) ;
305
+ std:: mem:: swap (
306
+ & mut cells[ i] . borrow_mut ( ) . elem ,
307
+ & mut cells[ j] . borrow_mut ( ) . elem ,
308
+ ) ;
311
309
}
312
310
i += 1 ;
313
311
}
@@ -459,6 +457,23 @@ impl<T: Default> LinkedList<T> {
459
457
self . head = previous;
460
458
}
461
459
460
+ pub fn reverse_recursively ( & mut self ) {
461
+ if self . len ( ) < 2 {
462
+ return ;
463
+ }
464
+ let cell = self . head . take ( ) ;
465
+ Self :: reverse_recursively_helper ( cell) ;
466
+ }
467
+
468
+ fn reverse_recursively_helper ( cell : Option < Cell < T > > ) {
469
+ if let Some ( cell) = cell {
470
+ let has_next = cell. borrow ( ) . next . is_some ( ) ;
471
+ if has_next {
472
+ Self :: reverse_recursively_helper ( cell. borrow_mut ( ) . next . take ( ) ) ;
473
+ }
474
+ }
475
+ }
476
+
462
477
///
463
478
///Append another list to this
464
479
///
@@ -765,7 +780,7 @@ impl<T: Default> LinkedList<T> {
765
780
self . head . as_mut ( ) . map ( |node| MutT ( node. borrow_mut ( ) ) )
766
781
}
767
782
///Quick sort - slow. Usage is advisable when list size is small
768
-
<
8000
/td>783
+
769
784
pub fn quicksort ( & self , ascending : bool )
770
785
where
771
786
T : PartialOrd ,
@@ -775,13 +790,8 @@ impl<T: Default> LinkedList<T> {
775
790
self . quicklysort ( ascending, 0 , self . len - 1 , & minivec) ;
776
791
}
777
792
778
- fn quicklysort (
779
- & self ,
780
- ascending : bool ,
781
- start : usize ,
782
- end : usize ,
783
- cells : & MiniVec < Cell < T > > ,
784
- ) where
793
+ fn quicklysort ( & self , ascending : bool , start : usize , end : usize , cells : & MiniVec < Cell < T > > )
794
+ where
785
795
T : PartialOrd ,
786
796
{
787
797
if start < end {
0 commit comments