aboutsummaryrefslogtreecommitdiff
path: root/middle_linked_list.rs
diff options
context:
space:
mode:
Diffstat (limited to 'middle_linked_list.rs')
-rw-r--r--middle_linked_list.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/middle_linked_list.rs b/middle_linked_list.rs
new file mode 100644
index 0000000..ed84409
--- /dev/null
+++ b/middle_linked_list.rs
@@ -0,0 +1,28 @@
+#[derive(PartialEq, Eq, Clone, Debug)]
+struct Solution;
+pub struct ListNode {
+ pub val: i32,
+ pub next: Option<Box<ListNode>>
+}
+
+impl ListNode {
+ #[inline]
+ fn new(val: i32) -> Self {
+ ListNode {
+ next: None,
+ val
+ }
+ }
+}
+
+impl Solution {
+ pub fn middle_node(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
+ let mut curr = &head;
+ let mut node_vec: Vec<Option<Box<ListNode>>> = vec![];
+ while let Some(node) = curr.as_ref() {
+ node_vec.push(Some(node.clone()));
+ curr = &curr.as_ref().unwrap().next;
+ }
+ node_vec[node_vec.len() / 2].clone()
+ }
+}