leetcode Remove Nth Node From End of List

class Solution {
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {
       ListNode *a=head,*b=head;
       int i=0;
       ListNode *pre=head;
       while(i<n)
       {
           a=a->next;
           i++;          
       }
       while(a!=NULL&&b!=NULL)
       {
           pre=b;
           a=a->next;
           b=b->next;
       }
       if(pre==b)
       {
         head=head->next;
         free(pre);
       }
       else
       {
           pre->next=b->next;
           free(b);
       }
       return head; 
    }
};
原文地址:https://www.cnblogs.com/tgkx1054/p/3152720.html