Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc5ecb1

Browse files
authored
feat: add rust solution to lc problem: No.0092 (#1673)
No.0092.Reverse Linked List II
1 parent 9e0a1c1 commit fc5ecb1

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

‎solution/0000-0099/0092.Reverse Linked List II/README.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,48 @@ function reverseBetween(head: ListNode | null, left: number, right: number): Lis
317317
}
318318
```
319319

320+
### **Rust**
321+
322+
```rust
323+
// Definition for singly-linked list.
324+
// #[derive(PartialEq, Eq, Clone, Debug)]
325+
// pub struct ListNode {
326+
// pub val: i32,
327+
// pub next: Option<Box<ListNode>>
328+
// }
329+
//
330+
// impl ListNode {
331+
// #[inline]
332+
// fn new(val: i32) -> Self {
333+
// ListNode {
334+
// next: None,
335+
// val
336+
// }
337+
// }
338+
// }
339+
impl Solution {
340+
pub fn reverse_between(head: Option<Box<ListNode>>, left: i32, right: i32) -> Option<Box<ListNode>> {
341+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
342+
let mut pre = &mut dummy;
343+
for _ in 1..left {
344+
pre = &mut pre.as_mut().unwrap().next;
345+
}
346+
let mut cur = pre.as_mut().unwrap().next.take();
347+
for _ in 0..right - left + 1 {
348+
let mut next = cur.as_mut().unwrap().next.take();
349+
cur.as_mut().unwrap().next = pre.as_mut().unwrap().next.take();
350+
pre.as_mut().unwrap().next = cur.take();
351+
cur = next;
352+
}
353+
for _ in 0..right - left + 1 {
354+
pre = &mut pre.as_mut().unwrap().next;
355+
}
356+
pre.as_mut().unwrap().next = cur;
357+
dummy.unwrap().next
358+
}
359+
}
360+
```
361+
320362
### **...**
321363

322364
```

‎solution/0000-0099/0092.Reverse Linked List II/README_EN.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,48 @@ function reverseBetween(head: ListNode | null, left: number, right: number): Lis
300300
}
301301
```
302302

303+
### **Rust**
304+
305+
```rust
306+
// Definition for singly-linked list.
307+
// #[derive(PartialEq, Eq, Clone, Debug)]
308+
// pub struct ListNode {
309+
// pub val: i32,
310+
// pub next: Option<Box<ListNode>>
311+
// }
312+
//
313+
// impl ListNode {
314+
// #[inline]
315+
// fn new(val: i32) -> Self {
316+
// ListNode {
317+
// next: None,
318+
// val
319+
// }
320+
// }
321+
// }
322+
impl Solution {
323+
pub fn reverse_between(head: Option<Box<ListNode>>, left: i32, right: i32) -> Option<Box<ListNode>> {
324+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
325+
let mut pre = &mut dummy;
326+
for _ in 1..left {
327+
pre = &mut pre.as_mut().unwrap().next;
328+
}
329+
let mut cur = pre.as_mut().unwrap().next.take();
330+
for _ in 0..right - left + 1 {
331+
let mut next = cur.as_mut().unwrap().next.take();
332+
cur.as_mut().unwrap().next = pre.as_mut().unwrap().next.take();
333+
pre.as_mut().unwrap().next = cur.take();
334+
cur = next;
335+
}
336+
for _ in 0..right - left + 1 {
337+
pre = &mut pre.as_mut().unwrap().next;
338+
}
339+
pre.as_mut().unwrap().next = cur;
340+
dummy.unwrap().next
341+
}
342+
}
343+
```
344+
303345
### **...**
304346

305347
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 reverse_between(head: Option<Box<ListNode>>, left: i32, right: i32) -> Option<Box<ListNode>> {
19+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
20+
let mut pre = &mut dummy;
21+
for _ in 1..left {
22+
pre = &mut pre.as_mut().unwrap().next;
23+
}
24+
let mut cur = pre.as_mut().unwrap().next.take();
25+
for _ in 0..right - left + 1 {
26+
let mut next = cur.as_mut().unwrap().next.take();
27+
cur.as_mut().unwrap().next = pre.as_mut().unwrap().next.take();
28+
pre.as_mut().unwrap().next = cur.take();
29+
cur = next;
30+
}
31+
for _ in 0..right - left + 1 {
32+
pre = &mut pre.as_mut().unwrap().next;
33+
}
34+
pre.as_mut().unwrap().next = cur;
35+
dummy.unwrap().next
36+
}
37+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /