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;
}
};
浙公网安备 33010602011771号