@@ -41,6 +41,29 @@ use crate::util::linked_list::ListNode;
4141pub struct Solution ;
4242
4343impl Solution {
44+ pub fn sort_list_with_vec ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
45+ if head. is_none ( ) || head. as_ref ( ) . unwrap ( ) . next . is_none ( ) {
46+ return head;
47+ }
48+
49+ let mut t = vec ! [ ] ;
50+ let mut head = head;
51+ while let Some ( mut n) = head {
52+ head = n. next . take ( ) ;
53+ t. push ( n) ;
54+ }
55+
56+ t. sort ( ) ;
57+
58+ let mut head = None ;
59+ while let Some ( mut item) = t. pop ( ) {
60+ item. next = head;
61+ head = Some ( item) ;
62+ }
63+
64+ head
65+ }
66+
4467 pub fn sort_list ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
4568 if head. is_none ( ) || head. as_ref ( ) . unwrap ( ) . next . is_none ( ) {
4669 return head;
@@ -68,20 +91,22 @@ impl Solution {
6891 let mut pre = & mut head_pointer;
6992 while h1. is_some ( ) || h2. is_some ( ) {
7093 if h1. is_none ( ) {
71- pre. next = h2. clone ( ) ;
94+ pre. next = h2. take ( ) ;
7295 break ;
7396 }
7497 if h2. is_none ( ) {
75- pre. next = h1. clone ( ) ;
98+ pre. next = h1. take ( ) ;
7699 break ;
77100 }
78101
79102 if h1. as_ref ( ) . unwrap ( ) . val < h2. as_ref ( ) . unwrap ( ) . val {
80- pre. next = h1. clone ( ) ;
81- h1 = h1. as_ref ( ) . unwrap ( ) . next . clone ( ) ;
103+ pre. next = Some ( Box :: new ( ListNode :: new ( h1. as_ref ( ) . unwrap ( ) . val ) ) ) ;
104+ // h1.clone();
105+ h1 = h1. as_mut ( ) . unwrap ( ) . next . take ( ) ;
82106 } else {
83- pre. next = h2. clone ( ) ;
84- h2 = h2. as_ref ( ) . unwrap ( ) . next . clone ( ) ;
107+ pre. next = Some ( Box :: new ( ListNode :: new ( h2. as_ref ( ) . unwrap ( ) . val ) ) ) ;
108+ // pre.next = h2.clone();
109+ h2 = h2. as_mut ( ) . unwrap ( ) . next . take ( ) ;
85110 }
86111 pre = pre. next . as_deref_mut ( ) . unwrap ( ) ;
87112 }
@@ -101,10 +126,10 @@ impl Solution {
101126 let mut fast = Some ( Box :: new ( slow. clone ( ) ) ) ;
102127 while fast. is_some ( ) && fast. as_ref ( ) . unwrap ( ) . next . is_some ( ) {
103128 slow = slow. next . as_mut ( ) . unwrap ( ) ;
104- fast = fast. as_ref ( ) . unwrap ( ) . next . as_ref ( ) . unwrap ( ) . next . clone ( ) ;
129+ fast = fast. as_mut ( ) . unwrap ( ) . next . as_mut ( ) . unwrap ( ) . next . take ( ) ;
105130 }
106- let head2 = slow. next . clone ( ) ;
107- slow. next = None ;
131+ let head2 = slow. next . take ( ) ;
132+ // slow.next = None;
108133
109134 ( head_pointer. next , head2)
110135 }
@@ -125,5 +150,22 @@ mod tests {
125150 Solution :: sort_list( linked_list:: to_list( vec![ -1 , 5 , 3 , 4 , 0 ] ) ) ,
126151 linked_list:: to_list( vec![ -1 , 0 , 3 , 4 , 5 ] )
127152 ) ;
153+ assert_eq ! (
154+ Solution :: sort_list( linked_list:: to_list( vec![ ] ) ) ,
155+ linked_list:: to_list( vec![ ] )
156+ ) ;
157+
158+ assert_eq ! (
159+ Solution :: sort_list_with_vec( linked_list:: to_list( vec![ 4 , 2 , 1 , 3 ] ) ) ,
160+ linked_list:: to_list( vec![ 1 , 2 , 3 , 4 ] )
161+ ) ;
162+ assert_eq ! (
163+ Solution :: sort_list_with_vec( linked_list:: to_list( vec![ ] ) ) ,
164+ linked_list:: to_list( vec![ ] )
165+ ) ;
166+ assert_eq ! (
167+ Solution :: sort_list_with_vec( linked_list:: to_list( vec![ -1 , 5 , 3 , 4 , 0 ] ) ) ,
168+ linked_list:: to_list( vec![ -1 , 0 , 3 , 4 , 5 ] )
169+ ) ;
128170 }
129171}
0 commit comments