实现方法:
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
// write code here
if (head == NULL)
return NULL;
ListNode* res = new ListNode(0);
res->next = head;
//当前节点
ListNode* cur = head;
//前节点
ListNode* pre = res;
ListNode* fast = head;
//先让fast快走N步;
while (n--)
fast = fast->next;
//快慢指针同步,快指针到底最后,慢指针正好到了倒数第N个位置
while (fast != NULL) {
fast = fast->next;
pre = cur;
cur = cur->next;
}
//删除该位置的节点
pre->next = cur->next;
//返回头节点
return res->next;
}
};



