leetcode算法题--反转链表
题目链接:https://leetcode-cn.com/problems/reverse-linked-list/
这个题目用迭代和递归的方法都能做,但是代码很精致,需要些脑洞。
1、迭代
ListNode* reverseList(ListNode* head) {ListNode *pre=NULL;ListNode *cur=head;ListNode *next;while(cur){next=cur->next;cur->next=pre;pre=cur;cur=next;}return pre;
}
2、递归
ListNode* reverseList(ListNode* head) {if(!head||!head->next){return head;}ListNode *tmp=reverseList(head->next);head->next->next=head;head->next=NULL;return tmp;
}
leetcode算法题--反转链表相关推荐
- leetcode算法题--反转链表 II★
原题链接:https://leetcode-cn.com/problems/reverse-linked-list-ii/ 1.头插法 ListNode* reverseBetween(ListNod ...
- leetcode算法题--反转链表★
原题链接: https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/ https://leetcode-cn.com/problems/re ...
- leetcode算法题--相交链表
原题链接: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ https://leetcode-cn.com/pro ...
- leetcode算法题--分隔链表
原题链接:https://leetcode-cn.com/problems/split-linked-list-in-parts/ vector<ListNode*> splitListT ...
- leetcode算法题--排序链表★
原题链接:https://leetcode-cn.com/problems/sort-list/ 1.归并排序(递归版) ListNode* sortList(ListNode* head) {if( ...
- leetcode算法题--重排链表★
原题链接:https://leetcode-cn.com/problems/reorder-list/ 1.map void reorderList(ListNode* head) {map<i ...
- leetcode算法题--环形链表 II★
原题链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/ 1.map ListNode *detectCycle(ListNode *he ...
- leetcode算法题--有序链表转换二叉搜索树★
原题链接:https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 1.二分法+递归 TreeNode* ...
- leetcode算法题--分隔链表★
原题链接:https://leetcode-cn.com/problems/partition-list/ 1.双指针 ListNode* partition(ListNode* head, int ...
最新文章
- ASP.NET Core 2.2中的Endpoint路由
- 利用追赶法来求解方程Ax=b的C++程序
- (原创)一个JavaScript Function Outliner插件 第三个版本 让你的JavaScript代码也支持折叠...
- unity 畸变_unity3d 几种镜头畸变
- python 去除读取txt输出时候的换行号
- ediplus 复制编辑一列_Excel中如何使用公式查找一列中的重复值并且在另一列里面列出来...
- 交换机路由器常用命令
- 448.找到所有数组中消失的数字
- Solidity编程 五 之 数据类型
- Android Gallery3D源码分析(一)
- ArcGIS拓扑小技巧:两个面矢量合并但不叠加
- 【ESP32 Arduino平衡小车制作】(一)霍尔编码器解码
- w7文件加密提示没启用服务器,win7文件夹设置密码没有密钥提示该怎么解决
- ASIC和FPGA设计流程
- 基于OCCT7.4的实验性CAD程序
- ICGC:国际肿瘤基因组协会简介
- T-Pot安装教程(保证能运行,附安装需要的所有东西清单)
- 通过uart串口和printf函数打印
- 思岚科技机器人底盘价格揭秘
- STM32F767 资料汇总
热门文章
- python零基础怎么学-零基础的小白怎么学python?
- 爬虫好学吗python-爬虫Python入门好学吗?学什么?
- 网页爬虫 python-python爬虫——爬取网页的中文
- 学python需要学数据库吗-学习python用什么数据库好?
- python脚本实例手机端-python链接手机用Python实现命令行闹钟脚本实例
- python语音在线编辑-Python如何实现文本转语音
- 在NVIDIA Jetson Xavier NX上安装llvmlite报错:No such file or directory: ‘llvm-config‘: ‘llvm-config‘
- RequestParam注解在required设置为true时失效
- java8中Optional的使用
- apache2 的https配置和代理https后端nodejs配置