递归方式反转链表
编辑:谯胜平 分类:程序与算法 标签:反转链表 发布时间:2021-04-13 浏览次数:893次
#include<stdio.h> #include<iostream> using namespace std; struct LinkNode{ int data; LinkNode *next; }; LinkNode *reverse(LinkNode *root){ if(root == nullptr || root -> next == nullptr){ return root; } LinkNode *head = reverse(root -> next); root -> next -> next = root; root -> next = nullptr; return head; } int main(){ int n = 5, num[5] = {1,2,3,4,5}; LinkNode *root = new LinkNode, *now = nullptr; now = root; for(int i = 0; i < n; i++){ LinkNode *tempNode = new LinkNode; tempNode -> data = num[i]; now -> next = tempNode; now = tempNode; } now -> next = nullptr; root = reverse(root -> next); now = root; for(int i = 0; i < n; i++){ cout << now -> data << " "; now = now -> next; } return 0; }
热门文章
文章标签
- web(1)
- 数据库索引(1)
- 栈(1)
- const(2)
- #define(1)
- 虚函数(1)
- 反转链表(2)
- 深拷贝(1)
- 浅拷贝(1)
- 快速排序(1)
- 线程(1)
- 线程模型(1)
- (41)
- LRU(1)
- C++11(1)
- 一致性哈希算法(1)
- CPU(1)
- malloc(1)
- 迭代器(1)
- linux下编译(1)
- 类模板(1)
- git(1)
- Linux(2)
- 学科评估(2)
- scanf(2)
- gets(1)
- getchar(1)
- 考研经验(1)
- printf(1)
- mysql(2)
- STL(2)
- 富文本编辑器(1)
- 闰月(1)
- vector(1)
- CA(3)
- HTTPS(1)
- 晴天的魔法乐园(1)
- 单例模式(1)
- 谷歌(1)
- unzip(1)
- gcc(1)
- ubuntu(1)
- getline()(1)
- 作息时间表(1)
友情链接