Skip to content

Commit 842ac8b

Browse files
authored
feat: add rust solution to lcof problem: No.06 (doocs#674)
1 parent be2b1b0 commit 842ac8b

File tree

1 file changed

+42
-0
lines changed
  • lcof/面试题06. 从尾到头打印链表

1 file changed

+42
-0
lines changed

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

+42
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ function reversePrint(head: ListNode | null): number[] {
241241

242242
### **Rust**
243243

244+
- 动态数组
245+
244246
```rust
245247
// Definition for singly-linked list.
246248
// #[derive(PartialEq, Eq, Clone, Debug)]
@@ -272,6 +274,46 @@ impl Solution {
272274
}
273275
```
274276

277+
- 遍历
278+
279+
```rust
280+
// Definition for singly-linked list.
281+
// #[derive(PartialEq, Eq, Clone, Debug)]
282+
// pub struct ListNode {
283+
// pub val: i32,
284+
// pub next: Option<Box<ListNode>>
285+
// }
286+
//
287+
// impl ListNode {
288+
// #[inline]
289+
// fn new(val: i32) -> Self {
290+
// ListNode {
291+
// next: None,
292+
// val
293+
// }
294+
// }
295+
// }
296+
impl Solution {
297+
pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
298+
let mut cur = &head;
299+
let mut n = 0;
300+
while let Some(node) = cur {
301+
cur = &node.next;
302+
n += 1;
303+
}
304+
305+
let mut arr = vec![0; n];
306+
let mut cur = head;
307+
while let Some(node) = cur {
308+
n -= 1;
309+
arr[n] = node.val;
310+
cur = node.next;
311+
}
312+
arr
313+
}
314+
}
315+
```
316+
275317
### **...**
276318

277319
```

0 commit comments

Comments
 (0)