//链表的就地逆置
#include "iostream"
using namespace std;
struct node
{
int data;
struct node *next;
};
int main()
{
int n;
struct node *head,*p, *r, *q;
head = (struct node *)malloc(sizeof(struct node));
q = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
p = head;
cin>>n; //n个数,依次输入
for (int i = 0; i<n ; i++)
{
r = (struct node *)malloc(sizeof(struct node));
cin>>r->data;
r->next = NULL;
p->next = r;
p = r;
}
//逆置
q = head->next;
p = q->next;
q->next = NULL;
while (p != NULL)
{
r = p->next;
p->next = q;
q = p;
p = r;
}
head->next=q;
//从头依次输出
while (q->next != NULL)
{
printf("%d", q->data);
printf(" ");
q = q->next;
}
printf("%d", q->data);
cout << endl;
return 0;
}