解题思路:
(1)双指针,新建头结点
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *lh=new ListNode(0);
lh->next=head;
ListNode *pre=nullptr,*slow=lh,*fast=lh;
for(int i=0;i<n;i++) {
fast=fast->next;
}
while(fast) {
pre=slow;
slow=slow->next;
fast=fast->next;
}
pre->next=slow->next;
return lh->next;
}
};