@@ -41,6 +41,29 @@ use crate::util::linked_list::ListNode;
41
41
pub struct Solution ;
42
42
43
43
impl 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
+
44
67
pub fn sort_list ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
45
68
if head. is_none ( ) || head. as_ref ( ) . unwrap ( ) . next . is_none ( ) {
46
69
return head;
@@ -68,20 +91,22 @@ impl Solution {
68
91
let mut pre = & mut head_pointer;
69
92
while h1. is_some ( ) || h2. is_some ( ) {
70
93
if h1. is_none ( ) {
71
- pre. next = h2. clone ( ) ;
94
+ pre. next = h2. take ( ) ;
72
95
break ;
73
96
}
74
97
if h2. is_none ( ) {
75
- pre. next = h1. clone ( ) ;
98
+ pre. next = h1. take ( ) ;
76
99
break ;
77
100
}
78
101
79
102
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 ( ) ;
82
106
} 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 ( ) ;
85
110
}
86
111
pre = pre. next . as_deref_mut ( ) . unwrap ( ) ;
87
112
}
@@ -101,10 +126,10 @@ impl Solution {
101
126
let mut fast = Some ( Box :: new ( slow. clone ( ) ) ) ;
102
127
while fast. is_some ( ) && fast. as_ref ( ) . unwrap ( ) . next . is_some ( ) {
103
128
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 ( ) ;
105
130
}
106
- let head2 = slow. next . clone ( ) ;
107
- slow. next = None ;
131
+ let head2 = slow. next . take ( ) ;
132
+ // slow.next = None;
108
133
109
134
( head_pointer. next , head2)
110
135
}
@@ -125,5 +150,22 @@ mod tests {
125
150
Solution :: sort_list( linked_list:: to_list( vec![ -1 , 5 , 3 , 4 , 0 ] ) ) ,
126
151
linked_list:: to_list( vec![ -1 , 0 , 3 , 4 , 5 ] )
127
152
) ;
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
+ ) ;
128
170
}
129
171
}
0 commit comments