/** 声明一个头指针便于统一操作 当链表中只有一个节点时,声明一个头指针可以统一操作。 */ class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { // write code here ListNode* fast = head,*pre = new ListNode(0); pre->next = head; head = pre; while(n--) fast = fast->next; while(fast){ pre = pre->next; fast = fast->next; } ListNode *r = pre->next; pre->next = r->next; delete r; return head->next; } };