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

<p>给你一个链表，删除链表的倒数第 <code>n</code><em> </em>个结点，并且返回链表的头结点。</p><p><strong>进阶：</strong>你能尝试使用一趟扫描实现吗？</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0019.Remove%20Nth%20Node%20From%20End%20of%20List/images/remove_ex1.jpg" 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>

## template

```python
class ListNode:
	def __init__(self, val=0, next=None):
		self.val = val
		self.next = next
class LinkList:
	def __init__(self):
		self.head=None
	def initList(self, data):
		self.head = ListNode(data[0])
		r=self.head
		p = self.head
		for i in data[1:]:
			node = ListNode(i)
			p.next = node
			p = p.next
		return r
	def	convert_list(self,head):
		ret = []
		if head == None:
			return
		node = head
		while node != None:
			ret.append(node.val)
			node = node.next
		return ret
class Solution:
	def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
		v = ListNode(0, head)
		handle = v
		index = []
		while v is not None:
			index.append(v)
			v = v.next
		pre = len(index)-n-1
		next = len(index)-n+1
		index[pre].next = index[next] if next >= 0 and next < len(
			index) else None
		return handle.next
# %%
l = LinkList()
list1 = [1,2,3,4,5]
head = l.initList(list1)
n = 2
s = Solution()
print(l.convert_list(s.removeNthFromEnd(head, n)))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```