#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}List;
List* init(int len)
{
List *h,*p,*r;
h=(List*)malloc(sizeof(List));
h->next=NULL;
int i;
for (i=0;i<len;i++)
{
p=(List*)malloc(sizeof(List));
p->data=i+1;
p->next=NULL;
if(!h->next)
h->next=p;
else
r->next=p;
r=p;
} /*建立链表*/
//p=h->next;
//while(p){
// printf("%d ",p->data);
//p=p->next;}
return h;
}
List* inver(List*head)
{
List *p,*r;
//h=(List*)malloc(sizeof(List));
p=head->next;
while(p->next)
{
r=p->next;
p->next=r->next;
r->next=head->next;
head->next=r;
}
p=head->next;
while(p){
printf("%d ",p->data);
p=p->next;}
return head;
} /*反转链表*/
main()
{
List *head,*head2,*p;
int len=5;
head=init(len);
p=head->next;
while(p){
printf("%d ",p->data);
p=p->next;}
head2=inver(head);
}