diff --git a/solution/0000-0099/0086.Partition List/README.md b/solution/0000-0099/0086.Partition List/README.md index e9a2d169425d6..e6bbca3cb44c1 100644 --- a/solution/0000-0099/0086.Partition List/README.md +++ b/solution/0000-0099/0086.Partition List/README.md @@ -214,6 +214,47 @@ var partition = function (head, x) { }; ``` +### **Rust** + +```rust +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn partition(head: Option>, x: i32) -> Option> { + let mut head = head; + let mut d1 = Some(Box::new(ListNode::new(0))); + let mut d2 = Some(Box::new(ListNode::new(0))); + let (mut t1, mut t2) = (&mut d1, &mut d2); + while let Some(mut node) = head { + head = node.next.take(); + if node.val < x { + t1.as_mut().unwrap().next = Some(node); + t1 = &mut t1.as_mut().unwrap().next; + } else { + t2.as_mut().unwrap().next = Some(node); + t2 = &mut t2.as_mut().unwrap().next; + } + } + t1.as_mut().unwrap().next = d2.unwrap().next; + d1.unwrap().next + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0086.Partition List/README_EN.md b/solution/0000-0099/0086.Partition List/README_EN.md index fee4f2b64b322..77c2ec81ba414 100644 --- a/solution/0000-0099/0086.Partition List/README_EN.md +++ b/solution/0000-0099/0086.Partition List/README_EN.md @@ -198,6 +198,47 @@ var partition = function (head, x) { }; ``` +### **Rust** + +```rust +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn partition(head: Option>, x: i32) -> Option> { + let mut head = head; + let mut d1 = Some(Box::new(ListNode::new(0))); + let mut d2 = Some(Box::new(ListNode::new(0))); + let (mut t1, mut t2) = (&mut d1, &mut d2); + while let Some(mut node) = head { + head = node.next.take(); + if node.val < x { + t1.as_mut().unwrap().next = Some(node); + t1 = &mut t1.as_mut().unwrap().next; + } else { + t2.as_mut().unwrap().next = Some(node); + t2 = &mut t2.as_mut().unwrap().next; + } + } + t1.as_mut().unwrap().next = d2.unwrap().next; + d1.unwrap().next + } +} +``` + ### **...** ``` diff --git a/solution/0000-0099/0086.Partition List/Solution.rs b/solution/0000-0099/0086.Partition List/Solution.rs new file mode 100644 index 0000000000000..6f4beb3366b2c --- /dev/null +++ b/solution/0000-0099/0086.Partition List/Solution.rs @@ -0,0 +1,36 @@ +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn partition(head: Option>, x: i32) -> Option> { + let mut head = head; + let mut d1 = Some(Box::new(ListNode::new(0))); + let mut d2 = Some(Box::new(ListNode::new(0))); + let (mut t1, mut t2) = (&mut d1, &mut d2); + while let Some(mut node) = head { + head = node.next.take(); + if node.val < x { + t1.as_mut().unwrap().next = Some(node); + t1 = &mut t1.as_mut().unwrap().next; + } else { + t2.as_mut().unwrap().next = Some(node); + t2 = &mut t2.as_mut().unwrap().next; + } + } + t1.as_mut().unwrap().next = d2.unwrap().next; + d1.unwrap().next + } +}