-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.rs
More file actions
72 lines (65 loc) · 1.51 KB
/
main.rs
File metadata and controls
72 lines (65 loc) · 1.51 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut new_head = None;
while let Some(mut node) = head {
head = node.next.take();
node.next = new_head;
new_head = Some(node);
}
new_head
}
pub fn reverse_list_1(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut pre = None;
let mut cur = head;
while let Some(mut node) = cur {
let next = node.next;
node.next = pre;
pre = Some(node);
cur = next;
}
pre
}
}
struct Solution;
#[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 }
}
}
fn main() {
println!("Hello, world!");
let mut n1 = ListNode::new(1);
let mut n2 = ListNode::new(2);
let mut n3 = ListNode::new(3);
let mut n4 = ListNode::new(4);
let n5 = ListNode::new(5);
n4.next = Some(Box::new(n5));
n3.next = Some(Box::new(n4));
n2.next = Some(Box::new(n3));
n1.next = Some(Box::new(n2));
let head = Some(Box::new(n1));
let result = Solution::reverse_list(head);
println!("result = {:?}", result);
}