https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
思路:双指针。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode myhead(0);
myhead.next=head;
ListNode *pre=&myhead,*nxt=head;
while(n--)
nxt=nxt->next;
while(nxt)
pre=pre->next,nxt=nxt->next;
nxt=pre->next;
pre->next=nxt->next;
delete(nxt);
return myhead.next;
}
};