File tree Expand file tree Collapse file tree 3 files changed +106
-0
lines changed
solution/0000-0099/0019.Remove Nth Node From End of List Expand file tree Collapse file tree 3 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -226,6 +226,43 @@ def remove_nth_from_end(head, n)
226
226
end
227
227
```
228
228
229
+ ### ** Rust**
230
+
231
+ ``` rust
232
+ // Definition for singly-linked list.
233
+ // #[derive(PartialEq, Eq, Clone, Debug)]
234
+ // pub struct ListNode {
235
+ // pub val: i32,
236
+ // pub next: Option<Box<ListNode>>
237
+ // }
238
+ //
239
+ // impl ListNode {
240
+ // #[inline]
241
+ // fn new(val: i32) -> Self {
242
+ // ListNode {
243
+ // next: None,
244
+ // val
245
+ // }
246
+ // }
247
+ // }
248
+ impl Solution {
249
+ pub fn remove_nth_from_end (head : Option <Box <ListNode >>, n : i32 ) -> Option <Box <ListNode >> {
250
+ let mut dummy = Some (Box :: new (ListNode { val : 0 , next : head }));
251
+ let mut slow = & mut dummy ;
252
+ let mut fast = & slow . clone ();
253
+ for _ in 0 ..= n {
254
+ fast = & fast . as_ref (). unwrap (). next;
255
+ }
256
+ while fast . is_some () {
257
+ fast = & fast . as_ref (). unwrap (). next;
258
+ slow = & mut slow . as_mut (). unwrap (). next;
259
+ }
260
+ slow . as_mut (). unwrap (). next = slow . as_mut (). unwrap (). next. as_mut (). unwrap (). next. take ();
261
+ dummy . unwrap (). next
262
+ }
263
+ }
264
+ ```
265
+
229
266
### ** ...**
230
267
231
268
```
Original file line number Diff line number Diff line change @@ -214,6 +214,43 @@ def remove_nth_from_end(head, n)
214
214
end
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 remove_nth_from_end (head : Option <Box <ListNode >>, n : i32 ) -> Option <Box <ListNode >> {
238
+ let mut dummy = Some (Box :: new (ListNode { val : 0 , next : head }));
239
+ let mut slow = & mut dummy ;
240
+ let mut fast = & slow . clone ();
241
+ for _ in 0 ..= n {
242
+ fast = & fast . as_ref (). unwrap (). next;
243
+ }
244
+ while fast . is_some () {
245
+ fast = & fast . as_ref (). unwrap (). next;
246
+ slow = & mut slow . as_mut (). unwrap (). next;
247
+ }
248
+ slow . as_mut (). unwrap (). next = slow . as_mut (). unwrap (). next. as_mut (). unwrap (). next. take ();
249
+ dummy . unwrap (). next
250
+ }
251
+ }
252
+ ```
253
+
217
254
### ** ...**
218
255
219
256
```
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 remove_nth_from_end ( head : Option < Box < ListNode > > , n : i32 ) -> Option < Box < ListNode > > {
19
+ let mut dummy = Some ( Box :: new ( ListNode { val : 0 , next : head } ) ) ;
20
+ let mut slow = & mut dummy;
21
+ let mut fast = & slow. clone ( ) ;
22
+ for _ in 0 ..=n {
23
+ fast = & fast. as_ref ( ) . unwrap ( ) . next ;
24
+ }
25
+ while fast. is_some ( ) {
26
+ fast = & fast. as_ref ( ) . unwrap ( ) . next ;
27
+ slow = & mut slow. as_mut ( ) . unwrap ( ) . next ;
28
+ }
29
+ slow. as_mut ( ) . unwrap ( ) . next = slow. as_mut ( ) . unwrap ( ) . next . as_mut ( ) . unwrap ( ) . next . take ( ) ;
30
+ dummy. unwrap ( ) . next
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments