链表均带有头结点
迭代法:p1,p2,p3三个指针
#include <bits/stdc++.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void initlist(LinkList &L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
LNode *s,*p;
p=L;
int x;
scanf("%d",&x);
while(x!=-1){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=NULL;
p->next=s;
p=s;
scanf("%d",&x);
}
}
void printlist(LinkList &L)
{
LNode *cur;
if(L==NULL||L->next==NULL) return;
cur=L->next;
while(cur!=NULL){
cout<<cur->data<<" ";
cur=cur->next;
}
cout<<endl;
}
void reverse(LinkList &L)
{
if(L==NULL||L->next==NULL) return;
LNode *p1,*p2,*p3;
p1=L->next;
p2=p1->next;
while(p2!=NULL){
p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
}
L->next->next=NULL;
L->next=p1;
}
int main()
{
LinkList L;
initlist(L);
printlist(L);
reverse(L);
printlist(L);
return 0;
}
就地逆置:p,q两个指针
#include <bits/stdc++.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void initlist(LinkList &L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
LNode *s,*p;
p=L;
int x;
scanf("%d",&x);
while(x!=-1){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=NULL;
p->next=s;
p=s;
scanf("%d",&x);
}
}
void printlist(LinkList &L)
{
LNode *cur;
if(L==NULL||L->next==NULL) return;
cur=L->next;
while(cur!=NULL){
cout<<cur->data<<" ";
cur=cur->next;
}
cout<<endl;
}
void reverse(LinkList &L)
{
if(L==NULL||L->next==NULL) return;
LNode *p,*q;
p=L->next;
q=p->next;
while(q!=NULL)
{
p->next=q->next;
q->next=L->next;
L->next=q;
q=p->next;
}
}
int main()
{
LinkList L;
initlist(L);
printlist(L);
reverse(L);
printlist(L);
return 0;
}
递归
#include <bits/stdc++.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void initlist(LinkList &L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
LNode *s,*p;
p=L;
int x;
scanf("%d",&x);
while(x!=-1){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=NULL;
p->next=s;
p=s;
scanf("%d",&x);
}
}
void printlist(LinkList &L)
{
LNode *cur;
if(L==NULL||L->next==NULL) return;
cur=L->next;
while(cur!=NULL){
cout<<cur->data<<" ";
cur=cur->next;
}
cout<<endl;
}
LinkList reverse(LinkList &head)
{
if(head==NULL||head->next==NULL) return head;
LinkList new_head=reverse(head->next);
head->next->next=head;
head->next=NULL;
return new_head;
}
int main()
{
LinkList L;
initlist(L);
printlist(L);
L->next=reverse(L->next);
printlist(L);
return 0;
}
头插法
#include <bits/stdc++.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void initlist(LinkList &L)
{
L=(LNode*)malloc(sizeof(LNode));
L->next=NULL;
LNode *s,*p;
p=L;
int x;
scanf("%d",&x);
while(x!=-1){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=NULL;
p->next=s;
p=s;
scanf("%d",&x);
}
}
void printlist(LinkList &L)
{
LNode *cur;
if(L==NULL||L->next==NULL) return;
cur=L->next;
while(cur!=NULL){
cout<<cur->data<<" ";
cur=cur->next;
}
cout<<endl;
}
LinkList reverse(LinkList &L)
{
if(L==NULL||L->next==NULL) return NULL;
LinkList new_L;
new_L=(LinkList)malloc(sizeof(LNode));
new_L->next=NULL;
int data;
LNode *p,*s;
p=L->next;
while(p!=NULL)
{
data=p->data;
s=(LNode*)malloc(sizeof(LNode));
s->data=data;
s->next=new_L->next;
new_L->next=s;
p=p->next;
}
return new_L;
}
int main()
{
LinkList L;
initlist(L);
printlist(L);
LinkList new_L=reverse(L);
printlist(new_L);
return 0;
}