当前位置:网站首页>Reverse linked list - head insertion inversion method
Reverse linked list - head insertion inversion method
2022-07-30 22:55:00 【Knights of the code】
(1)思想:是指在原有链表的基础上,依次将位于链表头部的节点摘下,然后采用从头部插入的方式生成一个新链表,则此链表即为原链表的反转版.
(2)实现(如下图):
At first the creation of a new head node,然后在创建一个temp指针.
link * new_head = (link*)malloc(sizeof(link));
new_head->elem = 0;
new_head->next = NULL;
link * temp = NULL;
The next step will result in the node in the list:
temp = head->next;
//将 temp 从 head 中摘除
head->next = head->next->next;
For the new list of head insert:
//将 temp 插入到 new_head 的头部
temp->next = new_head->next;
new_head->next = temp;
以此类推:
摘除2:
temp = head->next;
head->next = head->next->next;
插入2:
temp->next = new_head->next;
new_head->next = temp;
插入完成:
摘除3:
插入3:
摘除4:
(Mark end of cycle)
插入4:
插入END.
片段代码:
//头插法反转链表
link * head_reverse(link * head) {
//Add the head for a new node
link * new_head = (link*)malloc(sizeof(link));
new_head->elem = 0;
new_head->next = NULL;
link * temp = NULL;
if (head == NULL || head->next == NULL || head->next->next == NULL ) {
return head;
}
while (head->next != NULL)
{
temp = head->next;
//将 temp 从 head 中摘除
head->next = head->next->next;
//将 temp 插入到 new_head 的头部
temp->next = new_head->next;
new_head->next = temp;
}
return new_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;
}
void display(link *p) {
link* temp = p->next;//将temp指针重新指向首元结点
//只要temp指针指向的结点的next不是Null,就执行输出语句.
while (temp) {
printf("%d ", temp->elem);
temp = temp->next;
}
printf("\n");
}
//头插法反转链表
link * head_reverse(link * head) {
//Add the head for a new node
link * new_head = (link*)malloc(sizeof(link));
new_head->elem = 0;
new_head->next = NULL;
link * temp = NULL;
if (head == NULL || head->next == NULL || head->next->next == NULL ) {
return head;
}
while (head->next != NULL)
{
temp = head->next;
//将 temp 从 head 中摘除
head->next = head->next->next;
//将 temp 插入到 new_head 的头部
temp->next = new_head->next;
new_head->next = temp;
}
return new_head;
}
int main()
{
link*p=initLink();
printf("原始链表:");
display(p);
printf("反转链表:");
p=head_reverse(p);
display(p);
return 0;
}
输出结果:
若无头结点:
代码片段:
//头插法反转链表
link * head_reverse(link * head) {
link * new_head = NULL;
link * temp = NULL;
if (head == NULL || head->next == NULL) {
return head;
}
while (head != NULL)
{
temp = head;
//将 temp 从 head 中摘除
head = head->next;
//将 temp 插入到 new_head 的头部
temp->next = new_head;
new_head = temp;
}
return new_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;
}
void display(link *p) {
link* temp = p;//将temp指针重新指向头结点
//只要temp指针指向的结点的next不是Null,就执行输出语句.
while (temp) {
printf("%d ", temp->elem);
temp = temp->next;
}
printf("\n");
}
//头插法反转链表
link * head_reverse(link * head) {
link * new_head = NULL;
link * temp = NULL;
if (head == NULL || head->next == NULL) {
return head;
}
while (head != NULL)
{
temp = head;
//将 temp 从 head 中摘除
head = head->next;
//将 temp 插入到 new_head 的头部
temp->next = new_head;
new_head = temp;
}
return new_head;
}
int main()
{
link*p=initLink();
printf("原始链表:");
display(p);
printf("反转链表:");
p=head_reverse(p);
display(p);
return 0;
}
边栏推荐
- “由于找不到MSVCP140.dll,无法继续执行代码,重新安装程序可能会解决此问题等”解决方案
- 反转链表-头插反转法
- go版本升级
- Gxlcms audio novel system/novel listening system source code
- language code table
- d使用among的问题
- cmd (command line) to operate or connect to the mysql database, and to create databases and tables
- 2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
- Alibaba Cloud video on demand + project combat
- Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
猜你喜欢
随机推荐
2022.7.28
PS基础学习(一)
2021GDCPC Guangdong University Student Programming Competition B.Byfibonacci
【CTF】buuctf web 详解(持续更新)
StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
d违反常了吗
【Untitled】
解决一个Mysql的utf8编码导致的问题
MySql统计函数COUNT详解
PhpMetrics 使用
mysql remove duplicate data
Alibaba Cloud video on demand + project combat
grub 学习
Golang 切片删除指定元素的几种方法
proxy反向代理
正则表达式语法及使用
ZZULIOJ:1119: 数列有序
2sk2225代换3A/1500V中文资料【PDF数据手册】
抽象类和接口(学习笔记)
Compressing Deep Graph Neural Networks via Adversarial Knowledge Distillation