struct ListNode* reverse(struct ListNode* head)
{
struct ListNode* p=head;
struct ListNode *temp=NULL;
struct ListNode *cur2=NULL;
int i=0;
if(p==NULL|| p->next==NULL)
return p;
while(p){
temp=p->next;
p->next=cur2;
cur2=p;
p=temp;
}
return cur2;
}
struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
head= reverse2(head);
struct ListNode* p=head;
struct ListNode* temp=NULL;
struct ListNode* pre=NULL;
if(head == NULL||n <= 0)
return head;
if(n==1){
temp=head->next;
free(head);
head=temp;
}
while(p && n > 1)
{
pre=p;
p=p->next;
n--;
}
if(pre!=NULL){
pre->next=p->next;
free(p);
}
head= reverse2( head);
return head;
}
|