当前位置:网站首页>递归反转链表
递归反转链表
2022-04-22 06:15:00 【yinhua405】
// 单链表节点的结构
class ListNode
{
public:
int val;
ListNode* next;
ListNode(int x)
{
val = x;
next = NULL;
}
};
ListNode*reverse(ListNode* root, ListNode* &head)
{
if (root->next == NULL)
{
head = root;
return root;
}
else
{
ListNode* tmp = root->next;
root->next = NULL;
ListNode* node = reverse(tmp,head);
node->next = root;
return root;
}
}
ListNode* reverse2(ListNode* head) {
if (head->next == NULL)
return head;
ListNode* last = reverse2(head->next);
head->next->next = head;
head->next = NULL;
return last;
}
void test()
{
ListNode* node1 = new ListNode(1);
ListNode* node2 = new ListNode(2);
ListNode* node3 = new ListNode(3);
ListNode* node4 = new ListNode(4);
ListNode* node5 = new ListNode(5);
node1->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
ListNode* head = NULL;
ListNode* ret = reverse(node1,head);
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://blog.csdn.net/yinhua405/article/details/124031211
边栏推荐
- Why is the data stored in the leaf node of the non primary key index the primary key value
- 抽象类和抽象方法
- sql server快速入门
- 带环链表详解
- [number theory] congruence (4): univariate linear congruence equations (elimination of two, Chinese remainder theorem)
- 顺序表 增删查(找)
- 14 lines of code to complete arbitrary selection of image crawling
- 更新hdf之后无法找到接口映射
- The only storage area in the JVM where GC and oom will not occur
- 友情链接汇总qwq
猜你喜欢

Define a student class 1 to get the student's name: get_ Name() return type: STR 2 get student's age: get_ Age() return type: int 3 returns the highest score among the three subjects. get_ course()

(4) Character set in SQL Server (collation)

Suppose there is such a relationship between the weight and height of adults: height (CM) - 100 = standard weight (kg). If the difference between a person's weight and its standard weight is between p

LaTex中插入图片报错Unknown graphics extension: .1.jpg. }

Pycharm only needs five steps to improve the download speed by using Tsinghua image source
![Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss](/img/4e/34e2820ff8579007b20b33b27d8f1d.png)
Error: [HSI 55-1545], unable to generate fsbl normally, unable to read in MSS file, failed to close system mss

八大排序的思想及其代码

When latex uses the template, the caption title of the picture cannot be left aligned

Leetcode - 6 - (chaîne multiplicatrice, prochain élément plus grand < Ⅰ Ⅱ Ⅲ >, K liste de chaînes inversées)

Anaconda installation and use
随机推荐
面试官常问的,对象分配的一般过程及特殊情况
Define the class shape as the parent class, and define the method to calculate the perimeter and area in the class; (2) Define the shape subclass circle, with radius attribute and constant PI, and ove
LaTex中添加作者照片和作者简介
Detailed tree array template -- Theory and code implementation
Find a notepad file by yourself, find the data material by yourself, and count the times of three keywords or sentence entries in the whole text.
Interviewers often ask about the general process and special circumstances of object allocation
Vscode, this is enough
指针 结构体 const 小结
In the process of class loading, the allocation area of class variables is different from that of instance variables
Precautions for using feign to make remote calls
Beyond Compare“授权密钥已被吊销”的解决办法
ERROR: [Hsi 55-1545] ,无法正常生成fsbl,Unable to read in MSS file,Failed to closesw system.mss
浅谈时间复杂度与空间复杂度
LaTex用模板的时候图片的caption标题无法左对齐
Redis advanced
带环链表详解
1420 · 最小覆盖子串II
MySQL learning notes
Byte Summer Internship - 20220304
【数论】同余(七):快速幂、矩阵快速幂