//算法关键:将头节点后的节点移到头结点之前,并使最前面的节点为头结点
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct LNode)
struct LNode
{
int data;
struct LNode *next;
};
struct LNode *creat()
{struct LNode *head,*p1,*p2;
p1=p2=(struct LNode * )malloc(LEN);
int n=0;
printf("请依次输入链表1的数据,输入0时结束:\n");
scanf("%d",&p1->data);
head=NULL;
while(p1->data!=0)
{n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct LNode *)malloc(LEN);
scanf("%d",&p1->data);
}
p2->next=NULL;
return(head);
}
void print(struct LNode *head)
{
struct LNode *p;
printf("逆置后的链表为:\n");
p=head;
if(head!=NULL)
{
while(p!=NULL)
{printf("%d\n",p->data);
p=p->next;
}
}
}
int main()
{struct LNode *head,*p1,*p2;
p1=p2=head=creat();
while((p2->next)!=NULL) {
p2=p2->next;
p1->next=p2->next;
p2->next=head;
head=p2;
p2=p1;
}
print(head);
return 0;
}