Skip to content

Commit 516cacd

Browse files
authored
feat: add rust solution to lc problem: No.0086 (#1655)
No.0086.Partition List
1 parent 7410837 commit 516cacd

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

solution/0000-0099/0086.Partition List/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,47 @@ var partition = function (head, x) {
214214
};
215215
```
216216

217+
### **Rust**
218+
219+
```rust
220+
// Definition for singly-linked list.
221+
// #[derive(PartialEq, Eq, Clone, Debug)]
222+
// pub struct ListNode {
223+
// pub val: i32,
224+
// pub next: Option<Box<ListNode>>
225+
// }
226+
//
227+
// impl ListNode {
228+
// #[inline]
229+
// fn new(val: i32) -> Self {
230+
// ListNode {
231+
// next: None,
232+
// val
233+
// }
234+
// }
235+
// }
236+
impl Solution {
237+
pub fn partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
238+
let mut head = head;
239+
let mut d1 = Some(Box::new(ListNode::new(0)));
240+
let mut d2 = Some(Box::new(ListNode::new(0)));
241+
let (mut t1, mut t2) = (&mut d1, &mut d2);
242+
while let Some(mut node) = head {
243+
head = node.next.take();
244+
if node.val < x {
245+
t1.as_mut().unwrap().next = Some(node);
246+
t1 = &mut t1.as_mut().unwrap().next;
247+
} else {
248+
t2.as_mut().unwrap().next = Some(node);
249+
t2 = &mut t2.as_mut().unwrap().next;
250+
}
251+
}
252+
t1.as_mut().unwrap().next = d2.unwrap().next;
253+
d1.unwrap().next
254+
}
255+
}
256+
```
257+
217258
### **...**
218259

219260
```

solution/0000-0099/0086.Partition List/README_EN.md

+41
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,47 @@ var partition = function (head, x) {
198198
};
199199
```
200200

201+
### **Rust**
202+
203+
```rust
204+
// Definition for singly-linked list.
205+
// #[derive(PartialEq, Eq, Clone, Debug)]
206+
// pub struct ListNode {
207+
// pub val: i32,
208+
// pub next: Option<Box<ListNode>>
209+
// }
210+
//
211+
// impl ListNode {
212+
// #[inline]
213+
// fn new(val: i32) -> Self {
214+
// ListNode {
215+
// next: None,
216+
// val
217+
// }
218+
// }
219+
// }
220+
impl Solution {
221+
pub fn partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
222+
let mut head = head;
223+
let mut d1 = Some(Box::new(ListNode::new(0)));
224+
let mut d2 = Some(Box::new(ListNode::new(0)));
225+
let (mut t1, mut t2) = (&mut d1, &mut d2);
226+
while let Some(mut node) = head {
227+
head = node.next.take();
228+
if node.val < x {
229+
t1.as_mut().unwrap().next = Some(node);
230+
t1 = &mut t1.as_mut().unwrap().next;
231+
} else {
232+
t2.as_mut().unwrap().next = Some(node);
233+
t2 = &mut t2.as_mut().unwrap().next;
234+
}
235+
}
236+
t1.as_mut().unwrap().next = d2.unwrap().next;
237+
d1.unwrap().next
238+
}
239+
}
240+
```
241+
201242
### **...**
202243

203244
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 partition(head: Option<Box<ListNode>>, x: i32) -> Option<Box<ListNode>> {
19+
let mut head = head;
20+
let mut d1 = Some(Box::new(ListNode::new(0)));
21+
let mut d2 = Some(Box::new(ListNode::new(0)));
22+
let (mut t1, mut t2) = (&mut d1, &mut d2);
23+
while let Some(mut node) = head {
24+
head = node.next.take();
25+
if node.val < x {
26+
t1.as_mut().unwrap().next = Some(node);
27+
t1 = &mut t1.as_mut().unwrap().next;
28+
} else {
29+
t2.as_mut().unwrap().next = Some(node);
30+
t2 = &mut t2.as_mut().unwrap().next;
31+
}
32+
}
33+
t1.as_mut().unwrap().next = d2.unwrap().next;
34+
d1.unwrap().next
35+
}
36+
}

0 commit comments

Comments
 (0)