#include <iostream>
using namespace std;
struct Node{
int val;
Node* next;
Node(int x):val(x),next(nullptr){}
};
Node* Reverse(Node* head)
{
Node* pre=nullptr;
Node* p=head;
while(p)
{
Node* next=p->next;
p->next=pre;
pre=p;
p=next;
}
p=pre;
return p;
}
void printNode(Node* h)
{
Node* p=h;
while(p)
{
cout<<p->val<<" ";
p=p->next;
}
cout<<endl;
}
int main() {
Node *h=NULL;
h=new Node(1);
Node *p=h;
for(int i=2;i<=5;i++)
{
Node *s=new Node(i);
p->next=s;
p=s;
}
printNode(h);
h=Reverse(h);
printNode(h);
return 0;
}