#include <iostream>
using namespace std;
typedef struct List
{
int value;
struct List *next;
}list,*ListNode;
//反转
list *reverse(ListNode head)
{
//如果链表为空,反转还是空链表;如果链表里面只有一个元素,反转还是自身,所以直接返回。
if (head==NULL || head->next==NULL)
{
return head;
}
ListNode pre=head,cur=head->next;
pre->next=NULL;
while(cur!=NULL)
{
list *nxt=cur->next;
cur->next=pre;
pre=cur;
cur=nxt;
}
return pre;
}
//递归
list *reverse2(ListNode head)
{
if (head==NULL || head->next==NULL)
{
return head;
}
else
{
list *newhead=reverse2(head->next);
head->next->next=head;
head->next=NULL;
return newhead;
}
}
//打印
void printlist(ListNode lianbiao)
{
while(lianbiao!=NULL)
{
cout<<lianbiao->value<<" ";
lianbiao=lianbiao->next;
}
}
//头插法建立链表
void headinsert(ListNode &LB,int i)
{
list *p=(list*)malloc(sizeof(list));
p->value=i;
p->next=NULL;
if (LB==NULL)
{
LB=p;
}
else
{
p->next=LB;
LB=p;
}
}
```cpp
//尾插法建立链表
void tailinsert(list *&newlianbiao,int n,list *pold)
{
for (int i=0;i<n;i++)
{
List *pnew=(list*)malloc(sizeof(List));
pnew->value=i;
pnew->next=NULL;
if (newlianbiao==NULL)
{
newlianbiao=pnew;
pold=newlianbiao;
}
else
{
pold->next=pnew;
pold=pnew;
}
}
}
void main()
{
List *newlianbiao=NULL, *pold=NULL;
for (int i=0;i<9;i++)
{
List *pnew=(list*)malloc(sizeof(List));
pnew->value=i;
pnew->next=NULL;
if (newlianbiao==NULL)
{
newlianbiao=pnew;
pold=newlianbiao;
}
else
{
pold->next=pnew;
pold=pnew;
}
}
printlist(newlianbiao);
list *reverselianbiao=reverse2(newlianbiao);
printlist(reverselianbiao);
}
https://blog.csdn.net/geekmanong/article/details/51097196
https://blog.csdn.net/fx677588/article/details/72357389
https://blog.csdn.net/qq_42351880/article/details/88637387
https://blog.csdn.net/u014398524/article/details/104127722
https://www.cnblogs.com/xiaokang01/p/12409914.html(单链表的建立)