-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib.rs
76 lines (74 loc) · 1.71 KB
/
lib.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
/*
* @lc app=leetcode.cn id=19 lang=rust
*
* [19] 删除链表的倒数第N个节点
*/
// 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 }
}
}
struct Solution {}
// @lc code=start
// 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 remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
if head.is_none() {
return head;
}
let mut slow = &head;
let mut fast = &head;
let mut i = 1;
while i < n {
// 快指针先走n步
fast = &fast.as_ref()?.next;
i += 1;
}
while fast.as_ref()?.next.is_some() {
fast = &fast.as_ref()?.next;
slow = &slow.as_ref()?.next;
}
// 这里必须clone一下因为head已经borrow了,没办法再move
head = delete_node(head.clone(), slow);
head
}
}
fn delete_node(
head: Option<Box<ListNode>>,
target: &Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut phead = Some(Box::new(ListNode { val: 1, next: head }));
let mut root = &mut phead;
while root.as_mut()?.next.is_some() {
if &root.as_mut()?.next == target {
let target_next = &target.as_ref()?.next;
root.as_mut()?.next = target_next.clone();
break;
}
root = &mut root.as_mut()?.next;
}
phead?.next
}
// @lc code=end