当前位置:网站首页>Reverse linked list - recursive inversion method
Reverse linked list - recursive inversion method
2022-07-30 11:47:00 【code knight】
(1) Thought: Contrary to the idea of the iterative inversion method, the realization idea of the recursive inversion method is to start from the tail node of the linked list and traverse forward in turn. The traversal process changes the direction of each node in turn, that is, the otherpoints to the previous node.
(2) Implementation (it is recommended to read the code first, then the diagram to make the understanding more profound):
Start recursion:
New_head comes to the last node of the linked list after three recursive function calls, because new_head=head->next, so head always remains at the previous node of new_head.

When recursing to the last level:

Begin to recurse layer by layer:




End of recursion.
The final result is:

Code snippet:
link* recursive_reverse(link* head) {// out recursive conditionif (head == NULL || head->next == NULL){return head;}else{//new_head always finds the address of the last element in the linked listlink *new_head = recursive_reverse(head->next);//After recursion, put the linked listhead->next->next = head;head->next = NULL;return new_head;}}Complete code:
#include#includetypedef struct Link{int elem;struct Link* next;}link;link* initLink(){int i;//define the head pointerlink* head=NULL;//define the first nodelink* temp=(link*)malloc(sizeof(link));//Initialize the first nodetemp->elem=1;temp->next=NULL;head=temp;//create other nodesfor(i=2;i<5;i++){link* a=(link*)malloc(sizeof(link));a->elem=i;a->next=NULL;temp->next=a;temp=temp->next;}//return the address of the head pointerreturn head;}void printLink(link* head){link* p=head;while(p){printf("%d ",p->elem);p=p->next;}printf("\n");}link* recursive_reverse(link* head) {// out recursive conditionif (head == NULL || head->next == NULL){return head;}else{//new_head always finds the address of the last element in the linked listlink *new_head = recursive_reverse(head->next);//After recursion, put the linked listhead->next->next = head;head->next = NULL;return new_head;}}int main(){link*p=initLink();printf("Original linked list: ");printLink(p);printf("Reverse linked list: ");p=recursive_reverse(p);printLink(p);return 0;} Output result:

边栏推荐
- IO/多路复用(select/poll/epoll)
- TensorFlow自定义训练函数
- Hu-cang integrated e-commerce project (1): project background and structure introduction
- Verilog语法基础HDL Bits训练 08
- RY-D1/1电压继电器
- 干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
- Kubernetes 入门实战03 中级篇
- salesforce使用方法(salesforce authenticator下载)
- ODrive应用 #4 配置参数&指令「建议收藏」
- Microsoft SQL server hacked, bandwidth stolen
猜你喜欢

高能产出!腾讯内部的MyCat中间件手册,理论实操齐下

明德扬FPGA开发板XILINX-K7核心板Kintex7 XC7K325 410T工业级

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

高手云集、丰富活动,斩获佳绩,超过2万名开发者参与的AI社团邀你加入!

反转链表-递归反转法

Typroa alternative tool marktext

电流继电器JL-8GB/11/AC220V

电压继电器HDY-A/1-220VAC-1

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

VSCode更改插件的安装位置
随机推荐
真正懂经营管理的CIO具备哪些特质
零代码开发入门:快速上手DIY函数公式的5个步骤
向上管理读书笔记
LeetCode_235_Last Common Ancestor of Binary Search Tree
The use and principle of distributed current limiting reduction RRateLimiter
Difference between C# enumeration type and xaml
Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions
京东校招笔试题+知识点总结
横向对比5种常用的注册中心,无论是用于面试还是技术选型,都非常有帮助
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
FPGA刷题——计数器(简易秒表、可置位计数器、加减计数器)
eric6教程(电脑的配置基本知识)
contentDocument contentWindow,canvas 、svg,iframe
久经沙场的程序员居然也被某鱼的假程序员骗了,程序员之间的信任应该是最高的,他一个人毁了这种信任感
High energy output!Tencent's internal MyCat middleware manual, both theoretical and practical
单片机开发之LCD1602显示实验
win下怎么搭建php环境的方法教程
电压继电器HDY-A/1-220VAC-1
自定义查询--关于倒排索引的研究
LeetCode_235_二叉搜索树的最近公共祖先