#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
#define CLS(x,v) memset(x,v,sizeof(x))
#define LL long long
#define M 50001
struct Node
{
int data;
Node *next;
};
Node* create(int n){
if(n<1)return NULL;
Node* p=new Node;// 返回类型是Node *
p->data=1;
Node *head=p;
Node *temp;
for(int i=2;i<=n;i++)
{
temp=new Node;
temp->data=i;
p->next=temp;
p=temp;
}
p->next=NULL;
return head;
}
//反转链表效率O(n)
Node* myreverse(Node* head)
{
Node *one,*two,*three;
//初始化第一 二个
one=head;if(one==NULL)return NULL;//链表长度为0
two=one->next;if(two==NULL)return one;//链表长度为1
one->next=NULL;
while(1)
{
//根据地第二个产生第三个
three=two->next;
two->next=one;
if(three==NULL)break;
//前移 第一、二个指针
one=two;
two=three;
}
return two;
}
//查看链表
void show(Node *p)
{
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
//释放链表
void myfree(Node *p){
Node *temp;
while(p!=NULL)
{
temp=p->next;
delete p;
p=temp;
}
}
int main()
{
Node* head=create(5);
show(head);
//
head=myreverse(head);
show(head);
//
myfree(head);
return 0;
}