Skip to content

Commit b6f5a25

Browse files
authored
feat: add rust solution to lcof problem: No.06 (doocs#660)
1 parent 03e8853 commit b6f5a25

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

lcof/面试题06. 从尾到头打印链表/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,39 @@ function reversePrint(head: ListNode | null): number[] {
239239
}
240240
```
241241

242+
### **Rust**
243+
244+
```rust
245+
// Definition for singly-linked list.
246+
// #[derive(PartialEq, Eq, Clone, Debug)]
247+
// pub struct ListNode {
248+
// pub val: i32,
249+
// pub next: Option<Box<ListNode>>
250+
// }
251+
//
252+
// impl ListNode {
253+
// #[inline]
254+
// fn new(val: i32) -> Self {
255+
// ListNode {
256+
// next: None,
257+
// val
258+
// }
259+
// }
260+
// }
261+
impl Solution {
262+
pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
263+
let mut arr: Vec<i32> = vec![];
264+
let mut cur = head;
265+
while let Some(node) = cur {
266+
arr.push(node.val);
267+
cur = node.next;
268+
}
269+
arr.reverse();
270+
arr
271+
}
272+
}
273+
```
274+
242275
### **...**
243276

244277
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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_print(head: Option<Box<ListNode>>) -> Vec<i32> {
19+
let mut arr: Vec<i32> = vec![];
20+
let mut cur = head;
21+
while let Some(node) = cur {
22+
arr.push(node.val);
23+
cur = node.next;
24+
}
25+
arr.reverse();
26+
arr
27+
}
28+
}

0 commit comments

Comments
 (0)