File tree Expand file tree Collapse file tree 3 files changed +100
-0
lines changed
lcci/02.02.Kth Node From End of List Expand file tree Collapse file tree 3 files changed +100
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,41 @@ func kthToLast(head *ListNode, k int) int {
150
150
}
151
151
```
152
152
153
+ ### ** Rust**
154
+
155
+ ``` rust
156
+ // Definition for singly-linked list.
157
+ // #[derive(PartialEq, Eq, Clone, Debug)]
158
+ // pub struct ListNode {
159
+ // pub val: i32,
160
+ // pub next: Option<Box<ListNode>>
161
+ // }
162
+ //
163
+ // impl ListNode {
164
+ // #[inline]
165
+ // fn new(val: i32) -> Self {
166
+ // ListNode {
167
+ // next: None,
168
+ // val
169
+ // }
170
+ // }
171
+ // }
172
+ impl Solution {
173
+ pub fn kth_to_last (head : Option <Box <ListNode >>, k : i32 ) -> i32 {
174
+ let mut fast = & head ;
175
+ for _ in 0 .. k {
176
+ fast = & fast . as_ref (). unwrap (). next;
177
+ }
178
+ let mut slow = & head ;
179
+ while let (Some (f ), Some (s )) = (fast , slow ) {
180
+ fast = & f . next;
181
+ slow = & s . next;
182
+ }
183
+ slow . as_ref (). unwrap (). val
184
+ }
185
+ }
186
+ ```
187
+
153
188
### ** ...**
154
189
155
190
```
Original file line number Diff line number Diff line change @@ -142,6 +142,41 @@ func kthToLast(head *ListNode, k int) int {
142
142
}
143
143
```
144
144
145
+ ### ** Rust**
146
+
147
+ ``` rust
148
+ // Definition for singly-linked list.
149
+ // #[derive(PartialEq, Eq, Clone, Debug)]
150
+ // pub struct ListNode {
151
+ // pub val: i32,
152
+ // pub next: Option<Box<ListNode>>
153
+ // }
154
+ //
155
+ // impl ListNode {
156
+ // #[inline]
157
+ // fn new(val: i32) -> Self {
158
+ // ListNode {
159
+ // next: None,
160
+ // val
161
+ // }
162
+ // }
163
+ // }
164
+ impl Solution {
165
+ pub fn kth_to_last (head : Option <Box <ListNode >>, k : i32 ) -> i32 {
166
+ let mut fast = & head ;
167
+ for _ in 0 .. k {
168
+ fast = & fast . as_ref (). unwrap (). next;
169
+ }
170
+ let mut slow = & head ;
171
+ while let (Some (f ), Some (s )) = (fast , slow ) {
172
+ fast = & f . next;
173
+ slow = & s . next;
174
+ }
175
+ slow . as_ref (). unwrap (). val
176
+ }
177
+ }
178
+ ```
179
+
145
180
### ** ...**
146
181
147
182
```
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 kth_to_last ( head : Option < Box < ListNode > > , k : i32 ) -> i32 {
19
+ let mut fast = & head;
20
+ for _ in 0 ..k {
21
+ fast = & fast. as_ref ( ) . unwrap ( ) . next ;
22
+ }
23
+ let mut slow = & head;
24
+ while let ( Some ( f) , Some ( s) ) = ( fast, slow) {
25
+ fast = & f. next ;
26
+ slow = & s. next ;
27
+ }
28
+ slow. as_ref ( ) . unwrap ( ) . val
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments