当前位置:网站首页>Reverse linked list - in-place inversion method
Reverse linked list - in-place inversion method
2022-07-30 22:54:00 【code knight】
(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;//将tempA pointer to the first new point
//只要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;
}
边栏推荐
- 基于 Docker Compose 的 Nacos(MySQL 持久化)的搭建
- Difference between cookie and session
- [MySQL] Mysql transaction and authority management
- Navicat connection MySQL error: 1045 - Access denied for user 'root'@'localhost' (using password YES)
- cnpm installation steps
- CISP-PTE Zhenti Demonstration
- Day016 Classes and Objects
- 成功解决ImportError: cannot import name ‘_validate_lengths‘
- 力扣题(2)—— 两数相加
- Day016 类和对象
猜你喜欢
Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation
【云驻共创】HCSD大咖直播–就业指南
【MySQL】MySQL中对数据库及表的相关操作
[MySQL] Related operations on databases and tables in MySQL
【2022-05-31】JS逆向之易企秀
【科研】文献下载神器方式汇总
微软商店出现【0x800706D9】解决方法
IDEA usage skills
Go语学习笔记 - gorm使用 - 表增删改查 Web框架Gin(八)
抽象类和接口(学习笔记)
随机推荐
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
2022.7.27
【Untitled】
2022.7.28
MySQL 8.0.29 set and modify the default password
IDEA 连接 数据库
BFS题单总结
Regular expression syntax and usage
2022.7.27
打动中产精英群体,全新红旗H5用产品力跑赢需求
PhpMetrics usage
Navicat cannot connect to mysql super detailed processing method
language code table
MySQL连接时出现2003错误
2022.7.28
MYSQL JDBC Book Management System
cnpm installation steps
电脑快捷方式图标变白解决方案
力扣题(3)—— 无重复字符的最长子串
详解操作符