# 删除链表的倒数第 N 个结点

<p>给你一个链表，删除链表的倒数第 <code>n</code><em> </em>个结点，并且返回链表的头结点。</p><p><strong>进阶：</strong>你能尝试使用一趟扫描实现吗？</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/3ab1d7132da7283b4578c8df78c8fb67.jpeg" style="width: 542px; height: 222px;" /><pre><strong>输入：</strong>head = [1,2,3,4,5], n = 2<strong><br />输出：</strong>[1,2,3,5]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>head = [1], n = 1<strong><br />输出：</strong>[]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>head = [1,2], n = 1<strong><br />输出：</strong>[1]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>链表中结点的数目为 <code>sz</code></li>	<li><code>1 <= sz <= 30</code></li>	<li><code>0 <= Node.val <= 100</code></li>	<li><code>1 <= n <= sz</code></li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
struct ListNode
{
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};
#include <vector>
class Solution
{
public:
	ListNode *removeNthFromEnd(ListNode *head, int n)
	{
		ListNode empty_node(0, head);
		ListNode *p = &empty_node;
		std::vector<ListNode *> pv;
		while (p != nullptr)
		{
			pv.push_back(p);
			p = p->next;
		}
		_________________;
		p->next = p->next->next;
		return empty_node.next;
	}
};
```

## template

```cpp
struct ListNode
{
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};
#include <vector>
class Solution
{
public:
	ListNode *removeNthFromEnd(ListNode *head, int n)
	{
		ListNode empty_node(0, head);
		ListNode *p = &empty_node;
		std::vector<ListNode *> pv;
		while (p != nullptr)
		{
			pv.push_back(p);
			p = p->next;
		}
		p = pv[pv.size() - 1 - n];
		p->next = p->next->next;
		return empty_node.next;
	}
};
```

## 答案

```cpp
p = pv[pv.size() - 1 - n];
```

## 选项

### A

```cpp
p = pv[pv.size() + 1 - n];
```

### B

```cpp
p = pv[pv.size() - 1 + n];
```

### C

```cpp
p = pv[pv.size() + 1 + n];
```