当前位置:网站首页>Understanding recursion
Understanding recursion
2022-06-12 13:34:00 【You stole my name】
The meaning of recursion
The Chinese understanding of recursion is transfer and regression , Passing means passing something between each level of recursion , Regression is to go back to the first recursion . Remember that a little recursive process is doing the same thing over and over again .
How to write recursion ?
A common template for recursion is :① The termination condition of recursion , That is, when the recursion ends ;② What this round of recursion needs to do , That is, the processing of data , Including the value returned by the next level of recursion and the current round of value processing ;③ This recursion needs to return the value of the upper recursion .
example
Try your best to explain the topic , The title is as follows :
Recursion is always processed up from the end of recursion . So its code is :/**
- Definition for singly-linked list.
- struct ListNode {
int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}- };
/
// recursive
class Solution {
public:
ListNode swapPairs(ListNode* head) {
// Recursive termination condition
if(headnullptr||head->nextnullptr)
return head;
// What this level recursion needs to do
ListNode *node=swapPairs(head->next->next);
ListNode *temp=head->next;
head->next=node;
temp->next=head;
// Return the value of upper level recursion
return temp;
}
};
边栏推荐
- Application of short circuit expression (||) in C language
- Codeforces 1637 C. Andrew and stones - simple thinking
- 关于#SQLite写注册功能时,数据表查询出错#的问题,如何解决?
- JSP jump problem, unable to display database data, and unable to jump
- Debug code to quickly locate the error location
- C language [23] classic interview questions [2]
- Scyther工具形式化分析Woo-Lam协议
- jsp跳转问题,不能显示数据库数据,并且不能跳转
- 智能垃圾桶语音芯片应用设计方案介绍,WT588F02B-8S
- import torch_ Data view of geometric
猜你喜欢

Qualcomm platform development series (Protocol) QMI brief introduction and usage
![[brush title] probability of winning a draw](/img/f5/7e8dac944876f920db10508ec38e92.png)
[brush title] probability of winning a draw

高通平台开发系列讲解(协议篇)QMI简单介绍及使用方法

Experience and learning path of introductory deep learning and machine learning

leetcode 47. Permutations II 全排列 II(中等)

Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题

Web3.0,「激发创造」的时代

一种快速创建测试窗口的方法

import torch_geometric 的Data 查看

imagemagick:a gentle introduction to magick++
随机推荐
Codeforces 1629 F2. Game on sum (hard version) - Yang Hui's triangle, violence, finding rules
view的子视图的递归
将字符串转为16进制字符串并显示出来
C语言【23道】经典面试题【下】
JSP jump problem, unable to display database data, and unable to jump
Application of short circuit expression (||) in C language
The goods are full. You must take this knowledge
Title: Yanghui triangle
RK3399平台开发系列讲解(内核调试篇)2.50、systrace的使用
2062: [example 1.3] movie tickets
Software construction 03 regular expression
1004: character triangle
[cloud native | kubernetes] actual combat of ingress case
Innovation training (x) advanced interface beautification
Script引入CDN链接提示net::ERR_FILE_NOT_FOUND问题
Cocoapods的相关知识点
Codeforces 1637 C. Andrew and stones - simple thinking
GPUImage链式纹理的简单实现
list和dict的应用
Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver