-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlib.rs
More file actions
32 lines (29 loc) · 676 Bytes
/
lib.rs
File metadata and controls
32 lines (29 loc) · 676 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
struct Solution {}
fn middle_node(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut len: f64 = 0.0;
let mut root = &mut head;
while root.is_some() {
len += 1.0;
root = &mut root.as_mut().unwrap().next;
}
let middle_index = (len / 2.0).floor();
let mut i = 0.0;
root = &mut head;
while i < middle_index {
i += 1.0;
root = &mut root.as_mut().unwrap().next;
}
root.take()
}