File tree Expand file tree Collapse file tree 3 files changed +91
-0
lines changed
solution/0800-0899/0876.Middle of the Linked List Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change @@ -162,6 +162,38 @@ func middleNode(head *ListNode) *ListNode {
162
162
}
163
163
```
164
164
165
+ ### ** Rust**
166
+
167
+ ``` rust
168
+ // Definition for singly-linked list.
169
+ // #[derive(PartialEq, Eq, Clone, Debug)]
170
+ // pub struct ListNode {
171
+ // pub val: i32,
172
+ // pub next: Option<Box<ListNode>>
173
+ // }
174
+ //
175
+ // impl ListNode {
176
+ // #[inline]
177
+ // fn new(val: i32) -> Self {
178
+ // ListNode {
179
+ // next: None,
180
+ // val
181
+ // }
182
+ // }
183
+ // }
184
+ impl Solution {
185
+ pub fn middle_node (head : Option <Box <ListNode >>) -> Option <Box <ListNode >> {
186
+ let mut slow = & head ;
187
+ let mut fast = & head ;
188
+ while fast . is_some () && fast . as_ref (). unwrap (). next. is_some () {
189
+ slow = & slow . as_ref (). unwrap (). next;
190
+ fast = & fast . as_ref (). unwrap (). next. as_ref (). unwrap (). next;
191
+ }
192
+ slow . clone ()
193
+ }
194
+ }
195
+ ```
196
+
165
197
### ** ...**
166
198
167
199
```
Original file line number Diff line number Diff line change @@ -170,6 +170,38 @@ func middleNode(head *ListNode) *ListNode {
170
170
}
171
171
```
172
172
173
+ ### ** Rust**
174
+
175
+ ``` rust
176
+ // Definition for singly-linked list.
177
+ // #[derive(PartialEq, Eq, Clone, Debug)]
178
+ // pub struct ListNode {
179
+ // pub val: i32,
180
+ // pub next: Option<Box<ListNode>>
181
+ // }
182
+ //
183
+ // impl ListNode {
184
+ // #[inline]
185
+ // fn new(val: i32) -> Self {
186
+ // ListNode {
187
+ // next: None,
188
+ // val
189
+ // }
190
+ // }
191
+ // }
192
+ impl Solution {
193
+ pub fn middle_node (head : Option <Box <ListNode >>) -> Option <Box <ListNode >> {
194
+ let mut slow = & head ;
195
+ let mut fast = & head ;
196
+ while fast . is_some () && fast . as_ref (). unwrap (). next. is_some () {
197
+ slow = & slow . as_ref (). unwrap (). next;
198
+ fast = & fast . as_ref (). unwrap (). next. as_ref (). unwrap (). next;
199
+ }
200
+ slow . clone ()
201
+ }
202
+ }
203
+ ```
204
+
173
205
### ** ...**
174
206
175
207
```
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 middle_node ( head : Option < Box < ListNode > > ) -> Option < Box < ListNode > > {
19
+ let mut slow = & head;
20
+ let mut fast = & head;
21
+ while fast. is_some ( ) && fast. as_ref ( ) . unwrap ( ) . next . is_some ( ) {
22
+ slow = & slow. as_ref ( ) . unwrap ( ) . next ;
23
+ fast = & fast. as_ref ( ) . unwrap ( ) . next . as_ref ( ) . unwrap ( ) . next ;
24
+ }
25
+ slow. clone ( )
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments