* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
typedef struct ListNode* Node;
ListNode* removeNthFromEnd(ListNode* head, int n) {
if(!head) return head;
Node fast=head;
Node slow=head;
Node pre=nullptr;
for(int i=0;i<n;i++)
{
fast=fast->next;
}
if(!fast) return head->next;
while(fast)
{
pre=slow;
fast=fast->next;
slow=slow->next;
}
pre->next=slow->next;
return head;
}
};