/* 最长回文字串。 */ #include<stdio.h> #include<malloc.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<string.h> #include <iostream> typedef struct ListNode{ int val; struct ListNode *next; }; void print(struct ListNode *L){ struct ListNode *p=L->next; while(p){ printf("%d ",p->val); p=p->next; } printf(" "); } void create(struct ListNode *&L,int a[],int n){ struct ListNode *r,*s; L=(struct ListNode*)malloc(sizeof(struct ListNode)); r=L; int i; for(i=0;i<n;i++){ s=(struct ListNode*)malloc(sizeof(struct ListNode)); s->val=a[i]; r->next=s; r=s; } r->next=NULL; } struct ListNode* removeNthFromEnd(struct ListNode* head, int n){ struct ListNode* p,*q; p=q=head; if(!head||!head->next) return NULL; while(n){ q=q->next; n--; } if(!q) return p->next; q=q->next; while(q){ p=p->next; q=q->next; } p->next=p->next->next; return head; } int main() { int n=2,a[n]={1,2}; struct ListNode *L; create(L,a,n); print(L); removeNthFromEnd(L,2); print(L); return 0; }