File tree 3 files changed +118
-0
lines changed
solution/0000-0099/0086.Partition List
3 files changed +118
-0
lines changed Original file line number Diff line number Diff line change @@ -214,6 +214,47 @@ var partition = function (head, x) {
214
214
};
215
215
```
216
216
217
+ ### ** Rust**
218
+
219
+ ``` rust
220
+ // Definition for singly-linked list.
221
+ // #[derive(PartialEq, Eq, Clone, Debug)]
222
+ // pub struct ListNode {
223
+ // pub val: i32,
224
+ // pub next: Option<Box<ListNode>>
225
+ // }
226
+ //
227
+ // impl ListNode {
228
+ // #[inline]
229
+ // fn new(val: i32) -> Self {
230
+ // ListNode {
231
+ // next: None,
232
+ // val
233
+ // }
234
+ // }
235
+ // }
236
+ impl Solution {
237
+ pub fn partition (head : Option <Box <ListNode >>, x : i32 ) -> Option <Box <ListNode >> {
238
+ let mut head = head ;
239
+ let mut d1 = Some (Box :: new (ListNode :: new (0 )));
240
+ let mut d2 = Some (Box :: new (ListNode :: new (0 )));
241
+ let (mut t1 , mut t2 ) = (& mut d1 , & mut d2 );
242
+ while let Some (mut node ) = head {
243
+ head = node . next. take ();
244
+ if node . val < x {
245
+ t1 . as_mut (). unwrap (). next = Some (node );
246
+ t1 = & mut t1 . as_mut (). unwrap (). next;
247
+ } else {
248
+ t2 . as_mut (). unwrap (). next = Some (node );
249
+ t2 = & mut t2 . as_mut (). unwrap (). next;
250
+ }
251
+ }
252
+ t1 . as_mut (). unwrap (). next = d2 . unwrap (). next;
253
+ d1 . unwrap (). next
254
+ }
255
+ }
256
+ ```
257
+
217
258
### ** ...**
218
259
219
260
```
Original file line number Diff line number Diff line change @@ -198,6 +198,47 @@ var partition = function (head, x) {
198
198
};
199
199
```
200
200
201
+ ### ** Rust**
202
+
203
+ ``` rust
204
+ // Definition for singly-linked list.
205
+ // #[derive(PartialEq, Eq, Clone, Debug)]
206
+ // pub struct ListNode {
207
+ // pub val: i32,
208
+ // pub next: Option<Box<ListNode>>
209
+ // }
210
+ //
211
+ // impl ListNode {
212
+ // #[inline]
213
+ // fn new(val: i32) -> Self {
214
+ // ListNode {
215
+ // next: None,
216
+ // val
217
+ // }
218
+ // }
219
+ // }
220
+ impl Solution {
221
+ pub fn partition (head : Option <Box <ListNode >>, x : i32 ) -> Option <Box <ListNode >> {
222
+ let mut head = head ;
223
+ let mut d1 = Some (Box :: new (ListNode :: new (0 )));
224
+ let mut d2 = Some (Box :: new (ListNode :: new (0 )));
225
+ let (mut t1 , mut t2 ) = (& mut d1 , & mut d2 );
226
+ while let Some (mut node ) = head {
227
+ head = node . next. take ();
228
+ if node . val < x {
229
+ t1 . as_mut (). unwrap (). next = Some (node );
230
+ t1 = & mut t1 . as_mut (). unwrap (). next;
231
+ } else {
232
+ t2 . as_mut (). unwrap (). next = Some (node );
233
+ t2 = & mut t2 . as_mut (). unwrap (). next;
234
+ }
235
+ }
236
+ t1 . as_mut (). unwrap (). next = d2 . unwrap (). next;
237
+ d1 . unwrap (). next
238
+ }
239
+ }
240
+ ```
241
+
201
242
### ** ...**
202
243
203
244
```
Original file line number Diff line number Diff line change
1
+ // Definition for singly-linked list.
2
+ // #[derive(PartialEq, Eq, Clone, Debug)]
3
+ // pub struct ListNode {
4
+ // pub val: i32,
5
+ // pub next: Option<Box<ListNode>>
6
+ // }
7
+ //
8
+ // impl ListNode {
9
+ // #[inline]
10
+ // fn new(val: i32) -> Self {
11
+ // ListNode {
12
+ // next: None,
13
+ // val
14
+ // }
15
+ // }
16
+ // }
17
+ impl Solution {
18
+ pub fn partition ( head : Option < Box < ListNode > > , x : i32 ) -> Option < Box < ListNode > > {
19
+ let mut head = head;
20
+ let mut d1 = Some ( Box :: new ( ListNode :: new ( 0 ) ) ) ;
21
+ let mut d2 = Some ( Box :: new ( ListNode :: new ( 0 ) ) ) ;
22
+ let ( mut t1, mut t2) = ( & mut d1, & mut d2) ;
23
+ while let Some ( mut node) = head {
24
+ head = node. next . take ( ) ;
25
+ if node. val < x {
26
+ t1. as_mut ( ) . unwrap ( ) . next = Some ( node) ;
27
+ t1 = & mut t1. as_mut ( ) . unwrap ( ) . next ;
28
+ } else {
29
+ t2. as_mut ( ) . unwrap ( ) . next = Some ( node) ;
30
+ t2 = & mut t2. as_mut ( ) . unwrap ( ) . next ;
31
+ }
32
+ }
33
+ t1. as_mut ( ) . unwrap ( ) . next = d2. unwrap ( ) . next ;
34
+ d1. unwrap ( ) . next
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments