BJFUOJ
#include<bits/stdc++.h>
using namespace std;
typedef struct Lnode {
int data;
struct Lnode *next;
} Lnode, *Linklist;
void Init_Linklist(Linklist &l) {
l = new Lnode;
l->next = NULL;
}
void CreatLinklist(Linklist &l, int n) {
Linklist r = l;
Linklist p;
for (int i = 0; i < n; i++) {
p = new Lnode;
cin >> p->data;
p->next = r->next;
r->next = p;
r = p;
}
}
void Reverse(Linklist &l){
Linklist p = l->next;
l->next = NULL;
Linklist r;
while(p){
r = p->next;
p->next = l->next;
l->next = p;
p = r;
}
}
void OutPut(Linklist &l){
Linklist p = l->next;
while(p->next){
cout<<p->data<<" ";
p = p->next;
}
cout<<p->data<<endl;
}
int main() {
Linklist l;
int n;
cin>>n;
Init_Linklist(l);
CreatLinklist(l,n);
cout<<"原链表为:";
OutPut(l);
Reverse(l);
cout<<"逆转后的链表为:";
OutPut(l);
return 0;
}