一次遍历
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *dummy = new ListNode(0);
dummy->next = head;
ListNode *first = dummy;
ListNode *second = dummy;
for (int i = 1; i <= n+1; i++)//注意是循环n+1次
first = first->next;
while (first != NULL)
{
first = first->next;
second = second->next;
}
second->next = second->next->next;//直接覆盖
return dummy->next;
}
};