当前位置:网站首页>反转链表-迭代反转法
反转链表-迭代反转法
2022-07-30 11:23:00 【代码骑士】
反转链表,又可以称为翻转或逆置链表,如图:

常用的实现方案有 4 种,这里分别将它们称为迭代反转法、递归反转法、就地逆置法和头插法。值得一提的是,递归反转法更适用于反转不带头节点的链表;其它 3 种方法既能反转不带头节点的链表,也能反转带头节点的链表。
1、迭代反转链表:
(1)思想:从当前链表的首元节点开始,一直遍历至链表的最后一个节点,这期间会逐个改变所遍历到的节点的指针域,另其指向前一个节点。
(2)实现:借助 3 个指针分别命名为 beg、mid、end。它们的初始指向如图 所示:

接下来只需改变 mid 所指节点的指向即可,不用修改 3 个指针的指向。

最后只需改变头结点指针的指向,另其和 mid 同向,就实现了链表的反转。

代码片段:
//迭代反转链表
link* iteration_reverse(link* p){
if(p==NULL||p->next==NULL||p->next->next==NULL){
return p;
}
else{
link* beg=NULL;
link* mid=p->next;
link* end=p->next->next;
while(1){
//修改mid所指结点的指向
mid->next=beg;
//此时判断end是否为NULL,成立则退出循环
if(end==NULL){
break;
}
//三个指针整体向后移
beg=mid;
mid=end;
end=end->next;
}
//最后修改头指针指向
p->next=mid;
return p;
}
} 完整代码:
#include<stdio.h>
#include<stdlib.h>
//定义链表结点结构
typedef struct Link{
int elem;
struct Link* next;
}link;
//定义链表初始化函数
link* initLink(){
int i=0;
link* temp=NULL;
link* p=(link*)malloc(sizeof(link));//创建头结点
//头结点数据域为0
p->elem=0;
p->next=NULL;
temp=p;//头指针指向头结点
for(i=1;i<5;i++){
link* a=(link*)malloc(sizeof(link));
a->elem=i;
a->next=NULL;
temp->next=a;//连接结点
temp=a;//temp指针向后移动
}
return p;
}
//定义打印链表数据函数
void printLink(link* p){
link*t=p->next;//定义指针变量指向首元结点
while(t){
printf("%d ",t->elem);
t=t->next;
}
printf("\n");
}
//迭代反转链表
link* iteration_reverse(link* p){
if(p==NULL||p->next==NULL||p->next->next==NULL){
return p;
}
else{
link* beg=NULL;
link* mid=p->next;
link* end=p->next->next;
while(1){
//修改mid所指结点的指向
mid->next=beg;
//此时判断end是否为NULL,成立则退出循环
if(end==NULL){
break;
}
//三个指针整体向后移
beg=mid;
mid=end;
end=end->next;
}
//最后修改头指针指向
p->next=mid;
return p;
}
}
int main()
{
link* p=initLink();
printf("原始链表:");
printLink(p);
printf("反转链表:");
iteration_reverse(p);
printLink(p);
return 0;
}输出结果:

若无头结点:

代码片段:
//迭代反转法,head 为无头节点链表的头指针
link * iteration_reverse(link* head) {
if (head == NULL || head->next == NULL) {
return head;
}
else {
link * beg = NULL;
link * mid = head;
link * end = head->next;
//一直遍历
while (1)
{
//修改 mid 所指节点的指向
mid->next = beg;
//此时判断 end 是否为 NULL,如果成立则退出循环
if (end == NULL) {
break;
}
//整体向后移动 3 个指针
beg = mid;
mid = end;
end = end->next;
}
//最后修改 head 头指针的指向
head = mid;
return head;
}
}
完整代码:
#include <stdio.h>
#include <stdlib.h>
//链表中节点的结构
typedef struct Link {
int elem;
struct Link *next;
}link;
link * initLink() {
int i;
link * p = NULL;//创建头指针
link * temp = (link*)malloc(sizeof(link));//创建首元节点
//首元节点先初始化
temp->elem = 1;
temp->next = NULL;
p = temp;//头指针指向首元节点
for (i = 2; i < 5; i++) {
link *a = (link*)malloc(sizeof(link));
a->elem = i;
a->next = NULL;
temp->next = a;
temp = temp->next;
}
return p;
}
//迭代反转法,head 为无头节点链表的头指针
link * iteration_reverse(link* head) {
if (head == NULL || head->next == NULL) {
return head;
}
else {
link * beg = NULL;
link * mid = head;
link * end = head->next;
//一直遍历
while (1)
{
//修改 mid 所指节点的指向
mid->next = beg;
//此时判断 end 是否为 NULL,如果成立则退出循环
if (end == NULL) {
break;
}
//整体向后移动 3 个指针
beg = mid;
mid = end;
end = end->next;
}
//最后修改 head 头指针的指向
head = mid;
return head;
}
}
void display(link *p) {
link* temp = p;//将temp指针重新指向头结点
//只要temp指针指向的结点的next不是Null,就执行输出语句。
while (temp) {
printf("%d ", temp->elem);
temp = temp->next;
}
printf("\n");
}
int main() {
link*p = NULL;
//初始化链表(1,2,3,4)
printf("初始链表为:\n");
p = initLink();
display(p);
printf("反转链表为:\n");
p = iteration_reverse(p);
display(p);
return 0;
}
边栏推荐
- LeetCode_236_Last Common Ancestor of a Binary Tree
- 美团内推+校招笔试题+知识点总结
- STM32F1读取MLX90632非接触式红外温度传感器
- Explain the problem of change exchange in simple terms - the shell of the backpack problem
- Hu-cang integrated e-commerce project (1): project background and structure introduction
- Transfer Learning技术研修
- Static LED display developed by single chip microcomputer
- 数据库性能系列之索引(上)
- 域名怎么注册备案解析?
- TensorFlow custom training function
猜你喜欢

Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions

还在用Swagger?我推荐这款零代码侵入的接口管理神器

物联网技术概论:第6章

LeetCode_235_Last Common Ancestor of Binary Search Tree

Still using Swagger?I recommend this interface management artifact with zero code intrusion

C language - bitwise operations

真正懂经营管理的CIO具备哪些特质

久经沙场的程序员居然也被某鱼的假程序员骗了,程序员之间的信任应该是最高的,他一个人毁了这种信任感

Explain the problem of change exchange in simple terms - the shell of the backpack problem

Microsoft SQL服务器被黑客入侵 带宽被窃取
随机推荐
Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions
Explain the problem of change exchange in simple terms - the shell of the backpack problem
Meituan internal push + school recruitment written test + summary of knowledge points
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
自定义查询--关于倒排索引的研究
Verilog grammar basics HDL Bits training 07
TensorFlow自定义训练函数
Typroa alternative tool marktext
零代码开发入门:快速上手DIY函数公式的5个步骤
基于MySQL数据库,Redis缓存,MQ消息中间件,ES搜索引擎的高可用方案解析
[ASP.NET Core] Dependency Injection for Option Classes
Summary of text alignment, line height, space, etc.
"Learning Cloud Networking with Teacher Tang" - Problem Location - The host is working but the container is not working
API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试
Taobao/Tmall taobao comments q&a list interface API
Current relay JL-8GB/11/AC220V
电压继电器SRMUVS-100VAC-2H2D
【云筑共创】华为云携手鸿蒙,端云协同,培养创新型开发者
横向对比5种常用的注册中心,无论是用于面试还是技术选型,都非常有帮助
文本的对齐方式、行高、空间 等总结