Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rust solution to lc problem: No.0086 #1655

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions solution/0000-0099/0086.Partition List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
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
}
}
```

### **...**

```
Expand Down
41 changes: 41 additions & 0 deletions solution/0000-0099/0086.Partition List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
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
}
}
```

### **...**

```
Expand Down
36 changes: 36 additions & 0 deletions solution/0000-0099/0086.Partition List/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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
// }
// }
// }
impl Solution {
pub fn partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
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
}
}