# 19. Remove Nth Node From End of List

Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.

Example 1:

``````**Input:** head = [1,2,3,4,5], n = 2
**Output:** [1,2,3,5]
``````

Example 2:

``````**Input:** head = [1], n = 1
**Output:** []
``````

Example 3:

``````**Input:** head = [1,2], n = 1
**Output:** [1]
``````

Constraints:

• The number of nodes in the list is `sz`.
• `1 <= sz <= 30`
• `0 <= Node.val <= 100`
• `1 <= n <= sz`

Follow up: Could you do this in one pass?

``````class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
for (int i = 0; i < n; ++i) cur = cur->next;
while (cur->next) {
cur = cur->next;
pre = pre->next;
}
pre->next = pre->next->next;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/19

Swapping Nodes in a Linked List

Delete N Nodes After M Nodes of a Linked List

Delete the Middle Node of a Linked List

https://leetcode.com/problems/remove-nth-node-from-end-of-list/

https://leetcode.com/problems/remove-nth-node-from-end-of-list/discuss/8812/My-short-C%2B%2B-solution

https://leetcode.com/problems/remove-nth-node-from-end-of-list/discuss/8804/Simple-Java-solution-in-one-pass

LeetCode All in One 题目讲解汇总(持续更新中…)

（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，快快加入吧～）

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation