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

边栏推荐
- 电压继电器SRMUVS-100VAC-2H2D
- 《跟唐老师学习云网络》 - 问题定位 - 主机通但容器不通
- AB test summary
- [ASP.NET Core] Dependency Injection for Option Classes
- 久经沙场的程序员居然也被某鱼的假程序员骗了,程序员之间的信任应该是最高的,他一个人毁了这种信任感
- 分布式限流 redission RRateLimiter 的使用及原理
- 2022-07-29 顾宇佳 学习笔记 异常处理
- STM32F1读取MLX90632非接触式红外温度传感器
- Microsoft SQL server hacked, bandwidth stolen
- 208. 实现 Trie (前缀树)
猜你喜欢

Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions

Performance testing of API Gateway APISIX on Google Cloud T2A and T2D

Microsoft SQL server hacked, bandwidth stolen

VSCode更改插件的安装位置

Manage reading notes upward

RY-D1/1电压继电器

Verilog grammar basics HDL Bits training 07

ABP学习资源整理

The package of idea is not hollow

360闷声干大事获赞无数,数字安全如何保障?还得看企业安全云
随机推荐
又爆神作!阿里爆款MySQL高级宝典开源,直抵P7
Voltage relay h2d SRMUVS - 100 vac - 2
Verilog grammar basics HDL Bits training 08
contentDocument contentWindow, canvas, svg, iframe
FPGA刷题——计数器(简易秒表、可置位计数器、加减计数器)
PL5920 SOT-23-6 21V、2A、600KHz同步降压DC/DC转换器
Detailed explanation of @RequestBody and @ResponseBody
听到'演员工作比工人辛苦,吃得也不如工人好?'我笑了
向上管理读书笔记
ansible学习笔记01
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
不用if分支对同一个变量做判断的方法
24. 两两交换链表中的节点
获取1688app上原数据 API
汇编实现冒泡排序
Vim plugin GrepIt
Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions
contentDocument contentWindow,canvas 、svg,iframe
Typroa alternative tool marktext
基于滑模控制的不确定中立型系统有限时间稳定