当前位置:网站首页>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:

边栏推荐
- Difference between C# enumeration type and xaml
- 不用if分支对同一个变量做判断的方法
- 零代码开发入门:快速上手DIY函数公式的5个步骤
- decodeURIComponent()、eval()、encodeURIComponent()
- 单片机开发之ADC0808/9信号采集
- Transfer Learning技术研修
- Static LED display developed by single chip microcomputer
- MySQL——数据库基础
- Typroa alternative tool marktext
- 久经沙场的程序员居然也被某鱼的假程序员骗了,程序员之间的信任应该是最高的,他一个人毁了这种信任感
猜你喜欢
随机推荐
[Cloud-Building Co-creation] Huawei Cloud and Hongmeng collaborate to cultivate innovative developers
Underwater target detection method based on spatial feature selection
Scheduling of combined electric-heating system based on multi-objective two-stage stochastic programming method
获取1688app上原数据 API
TensorFlow自定义训练函数
云原生应用的概念和云原生应用的 15 个特征
Hu-cang integrated e-commerce project (1): project background and structure introduction
High energy output!Tencent's internal MyCat middleware manual, both theoretical and practical
基于声信道分析的电缆隧道人员定位技术
How to add data to the request header when feign is called remotely
Microsoft SQL服务器被黑客入侵 带宽被窃取
Testability of Fuzzy Discrete Event Systems
Verilog语法基础HDL Bits训练 08
ORA-00600 [13013], [5001], [268] 问题分析及恢复
decodeURIComponent()、eval()、encodeURIComponent()
LeetCode_235_二叉搜索树的最近公共祖先
VSCode更改插件的安装位置
C# 枚举类型 于xaml 中区别
pg_rewind 修复主备环境的时间线
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)









