#include <iostream>
using namespace std;
typedef struct node
{
int value;
struct node* next;
node(){next=NULL;}
node(int v,node* n){value=v;next=n;}
}node;
void insert(node*& head,int a[],int n)//??????????????????注意要用引用
{
node* rear=NULL;
for(int i=0;i<n;i++)
{
if(!head)
rear=head=new node(a[i],NULL);
else
rear=rear->next=new node(a[i],NULL);
}
}
node* reverse(node* head)
{
if(head){
node* p1=head,*p2,*p3,*temp;// 1 2 3 4 5 6 7 8 9 0, p1->1,p2->2, p3保存p2下一个节点的位置,每循环一个,p1指向p3在开始,而p1这次应该指向上次循环的p2
// 保存为temp;
while(1)
{
p2=p1->next;
if(!p2)
break;
p3=p2->next;
p2->next=p1;
if(p1==head)
p1->next=NULL;
else
{
p1->next=temp;
}
if(p3)
p1=p3;
else
return p2;//我草,当p3==null时,p1就为null了,不能在循环了,直接返回p2
temp=p2;
}
return p1;
}
else{
return NULL;}
}
int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,0};
node* head=NULL;
insert(head,a,10);
node* head2=reverse(head);
while(head2)
{
cout<<head2->value<<" ";
head2=head2->next;
}
return 0;
}