单链表逆置(四种方法),带头结点

该博客展示了四种不同的链表翻转方法:迭代法、就地逆置、递归和头插法。每种方法都包括代码实现,并通过初始化、打印链表和翻转后的打印来验证算法的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链表均带有头结点

迭代法: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值