当前位置:网站首页>反转链表-就地逆置法
反转链表-就地逆置法
2022-07-30 22:45:00 【代码骑士】
(1)思想:就地逆置法和头插法的实现思想类似,唯一的区别在于,头插法是通过建立一个新链表实现的,而就地逆置法则是直接对原链表做修改,从而实现将原链表反转。
(2)实现:在原链表的基础上做修改,需要额外借助 2 个指针(假设分别为 beg 和 end)。
END
代码片段:
//就地逆置法
link * local_reverse(link * head) {
link * beg = NULL;
link * end = NULL;
if (head == NULL || head->next == NULL || head->next->next == NULL) {
return head;
}
beg = head->next;
end = head->next->next;
while (end != NULL) {
//将 end 从链表中摘除
beg->next = end->next;
//将 end 移动至链表头
end->next = head->next;
head->next = end;
//调整 end 的指向,另其指向 beg 后的一个节点,为反转下一个节点做准备
end = beg->next;
}
return head;
}
完整代码:
#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));//创建头节点
//首元节点先初始化
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 = temp->next;
}
return p;
}
//就地逆置法
link * local_reverse(link * head) {
link * beg = NULL;
link * end = NULL;
if (head == NULL || head->next == NULL || head->next->next == NULL) {
return head;
}
beg = head->next;
end = head->next->next;
while (end != NULL) {
//将 end 从链表中摘除
beg->next = end->next;
//将 end 移动至链表头
end->next = head->next;
head->next = end;
//调整 end 的指向,另其指向 beg 后的一个节点,为反转下一个节点做准备
end = beg->next;
}
return head;
}
void display(link *p) {
link* temp = p->next;//将temp指针重新指向首元结点
//只要temp指针指向的结点的next不是Null,就执行输出语句。
while (temp) {
printf("%d ", temp->elem);
temp = temp->next;
}
printf("\n");
}
int main()
{
link*p=initLink();
printf("原始链表:");
display(p);
printf("反转链表:");
p=local_reverse(p);
display(p);
return 0;
}
输出结果:
若无头结点:
代码片段:
//就地逆置法
link * local_reverse(link * head) {
link * beg = NULL;
link * end = NULL;
if (head == NULL || head->next == NULL) {
return head;
}
beg = head;
end = head->next;
while (end != NULL) {
//将 end 从链表中摘除
beg->next = end->next;
//将 end 移动至链表头
end->next = head;
head = end;
//调整 end 的指向,另其指向 beg 后的一个节点,为反转下一个节点做准备
end = beg->next;
}
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;
}
//就地逆置法
link * local_reverse(link * head) {
link * beg = NULL;
link * end = NULL;
if (head == NULL || head->next == NULL) {
return head;
}
beg = head;
end = head->next;
while (end != NULL) {
//将 end 从链表中摘除
beg->next = end->next;
//将 end 移动至链表头
end->next = head;
head = end;
//调整 end 的指向,另其指向 beg 后的一个节点,为反转下一个节点做准备
end = beg->next;
}
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=initLink();
printf("原始链表:");
display(p);
printf("反转链表:");
p=local_reverse(p);
display(p);
return 0;
}
边栏推荐
- MYSQL JDBC图书管理系统
- Gxlcms audio novel system/novel listening system source code
- TransGAN代码复现—九天毕昇平台
- Installation and use of cnpm
- [MySQL] DQL related operations
- Navicat new database
- Collapse legacy apps
- VS2017 compile Tars test project
- ThinkPHP高仿蓝奏云网盘系统源码/对接易支付系统程序
- MySQL compressed package installation, fool teaching
猜你喜欢
Detailed explanation of the delete problem of ClickHouse delete data
MySQL 8.0.29 设置和修改默认密码
【MySQL】DQL相关操作
MySQL Soul 16 Questions, How Many Questions Can You Last?
2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】
[MySQL] Related operations on databases and tables in MySQL
Uni-app 小程序 App 的广告变现之路:激励视频广告
TransGAN代码复现—九天毕昇平台
MySQL连接时出现2003错误
It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)
随机推荐
Difference between cookie and session
ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c
QT开发简介、命名规范、signal&slot信号槽
VS2017 compile Tars test project
TCP 连接 三次握手 四次挥手
【MySQL】MySQL中对数据库及表的相关操作
Day 16 of HCIP
阿里云视频点播+项目实战
3 minutes to take you to understand WeChat applet development
WSL安装图形界面并通过xrdp/X-Launch访问
2sk2225 Substitute 3A/1500V Chinese Documentation【PDF Data Book】
The Road to Ad Monetization for Uni-app Mini Program Apps: Rewarded Video Ads
1064 Complete Binary Search Tree
CISP-PTE Zhenti Demonstration
正则表达式语法及使用
Golang 切片删除指定元素的几种方法
EasyExcel综合课程实战
【高等数学】矩阵与向量组的秩和等价
TransGAN code reproduction - Jiutian Bisheng Platform
2022.7.28