#include<iostream>
using namespace std;
typedef struct node
{
int data;
node* next;
};
struct node* reves(struct node* head)
{
node* pre, *cur, *pp;
pre = head;
cur = head->next;
pre->next = NULL;
while (cur!=NULL)
{
pp = cur->next;
cur->next = pre;
pre = cur;
cur = pp;
}
return pre;
}
int main()
{
/*构造链表 5,7,3,11,1,7
然后调用reverse逆置,再打印出来*/
node* a = new node;
node* head = a;//记录头结点
a->next = NULL;
int dt;
int i = 2;
cin >> i;
while (i--)
{
cin >> dt;
node* p = new node;
p->data = dt;
p->next = NULL;
a->next = p;
a = p;
}
node* res = reves(head->next);
while (res != NULL)
{
cout << res->data << " ";
res=res->next;
}
return 0;
}
翻转链表,死过一次,再死就是猪
于 2020-09-12 10:28:49 首次发布