-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathreverse_list_n.rs
82 lines (73 loc) · 2.1 KB
/
reverse_list_n.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
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
73
74
75
76
77
78
79
80
81
82
use super::ListNode2;
use std::cell::RefCell;
use std::rc::Rc;
pub fn reverse_list_n(
head: Option<Rc<RefCell<ListNode2>>>,
n: i32,
) -> Option<Rc<RefCell<ListNode2>>> {
let (last, _) = helper(head, n);
last
}
fn helper(
node: Option<Rc<RefCell<ListNode2>>>,
n: i32,
) -> (
Option<Rc<RefCell<ListNode2>>>,
Option<Rc<RefCell<ListNode2>>>,
) {
if n <= 0 || (node.is_none() || node.as_ref().unwrap().borrow().next.is_none()) {
// 边界条件
return (node.clone(), node.clone());
}
if n == 1 {
// 递归退出条件
return (node.clone(), node.as_ref().unwrap().borrow().next.clone());
}
let (last, successor) = helper(node.as_ref().unwrap().borrow().next.clone(), n - 1);
node.as_ref()
.unwrap()
.borrow()
.next
.as_ref()
.unwrap()
.borrow_mut()
.next = node.clone();
node.as_ref().unwrap().borrow_mut().next = successor.clone();
(last, successor)
}
#[cfg(test)]
mod tests {
use super::super::ListNode2;
use super::*;
#[test]
fn test_reverse_list_n() {
let ret = reverse_list_n(None, 0);
assert!(ret.is_none());
let ret = reverse_list_n(
Some(Rc::new(RefCell::new(ListNode2 {
val: 1,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 2,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 3,
next: Some(Rc::new(RefCell::new(ListNode2 { val: 4, next: None }))),
}))),
}))),
}))),
2,
);
assert_eq!(
ret,
Some(Rc::new(RefCell::new(ListNode2 {
val: 2,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 1,
next: Some(Rc::new(RefCell::new(ListNode2 {
val: 3,
next: Some(Rc::new(RefCell::new(ListNode2 { val: 4, next: None }))),
}))),
}))),
})))
)
}
}