<span style="font-size:18px;">#include <iostream>
#include <time.h>
#include <string>
using namespace std;
//单链表反转
struct node
{
int data;
node *next;
};
void Create(node * &head)
{
int data;
cin>>data;
node *p1,*p2;
while(data!=-1)
{
if(head==NULL)
{
head=new node;
head->data=data;
head->next=NULL;
p1=head;
}
else
{
p2=new node;
p2->data=data;
p1->next=p2;
p1=p2;
p1->next=NULL;
}
cin>>data;
}
}
void Print(node *head)
{
while(head!=NULL)
{
cout<<head->data<<' ';
head=head->next;
}
}
node *Reverse(node *p,node *&head)
{
if(p->next==NULL)
{
head->next=NULL;
head=p;
return p;
}
else
{
node *tmp=Reverse(p->next,head);
tmp->next=p;
return p;
}
}
void main()
{
node *head=NULL;
Create(head);
Reverse(head,head);
Print(head);
}
</span>