-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path206.反转链表.rs
43 lines (42 loc) · 883 Bytes
/
206.反转链表.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* @lc app=leetcode.cn id=206 lang=rust
*
* [206] 反转链表
*/
// 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 reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
match head {
Some(mut p) => {
let rev = Solution::reverse_list(p.clone().next);
// println!("{:?}", p);
// println!("{:?}", rev);
// rev.next = p.next
p.next = None;
match rev {
Some(mut pp) => {
pp.next = Some(p);
Some(pp)
}
None => Some(p),
}
}
None => None,
}
}
}