-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.删除链表的倒数第-n-个结点.rs
80 lines (77 loc) · 1.36 KB
/
19.删除链表的倒数第-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
/*
* @lc app=leetcode.cn id=19 lang=rust
*
* [19] 删除链表的倒数第 N 个结点
*
* https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/description/
*
* algorithms
* Medium (43.02%)
* Likes: 1626
* Dislikes: 0
* Total Accepted: 543.1K
* Total Submissions: 1.3M
* Testcase Example: '[1,2,3,4,5]\n2'
*
* 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
*
* 进阶:你能尝试使用一趟扫描实现吗?
*
*
*
* 示例 1:
*
*
* 输入:head = [1,2,3,4,5], n = 2
* 输出:[1,2,3,5]
*
*
* 示例 2:
*
*
* 输入:head = [1], n = 1
* 输出:[]
*
*
* 示例 3:
*
*
* 输入:head = [1,2], n = 1
* 输出:[1]
*
*
*
*
* 提示:
*
*
* 链表中结点的数目为 sz
* 1
* 0
* 1
*
*
*/
// @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(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
let mut pre_head: Option<Box<ListNode>> = Some(Box::new(head));
}
}
// @lc code=end