Skip to content

Commit ec80cd3

Browse files
committed
feat: add rust solution to lc problem: No.0876
No.0876.Middle of the Linked List
1 parent c0afbbf commit ec80cd3

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

solution/0800-0899/0876.Middle of the Linked List/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,38 @@ func middleNode(head *ListNode) *ListNode {
162162
}
163163
```
164164

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+
165197
### **...**
166198

167199
```

solution/0800-0899/0876.Middle of the Linked List/README_EN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,38 @@ func middleNode(head *ListNode) *ListNode {
170170
}
171171
```
172172

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+
173205
### **...**
174206

175207
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

0 commit comments

Comments
 (0)