题目链接: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算法题--反转链表相关推荐

  1. leetcode算法题--反转链表 II★

    原题链接:https://leetcode-cn.com/problems/reverse-linked-list-ii/ 1.头插法 ListNode* reverseBetween(ListNod ...

  2. leetcode算法题--反转链表★

    原题链接: https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/ https://leetcode-cn.com/problems/re ...

  3. leetcode算法题--相交链表

    原题链接: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ https://leetcode-cn.com/pro ...

  4. leetcode算法题--分隔链表

    原题链接:https://leetcode-cn.com/problems/split-linked-list-in-parts/ vector<ListNode*> splitListT ...

  5. leetcode算法题--排序链表★

    原题链接:https://leetcode-cn.com/problems/sort-list/ 1.归并排序(递归版) ListNode* sortList(ListNode* head) {if( ...

  6. leetcode算法题--重排链表★

    原题链接:https://leetcode-cn.com/problems/reorder-list/ 1.map void reorderList(ListNode* head) {map<i ...

  7. leetcode算法题--环形链表 II★

    原题链接:https://leetcode-cn.com/problems/linked-list-cycle-ii/ 1.map ListNode *detectCycle(ListNode *he ...

  8. leetcode算法题--有序链表转换二叉搜索树★

    原题链接:https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 1.二分法+递归 TreeNode* ...

  9. leetcode算法题--分隔链表★

    原题链接:https://leetcode-cn.com/problems/partition-list/ 1.双指针 ListNode* partition(ListNode* head, int ...

最新文章

  1. ASP.NET Core 2.2中的Endpoint路由
  2. 利用追赶法来求解方程Ax=b的C++程序
  3. (原创)一个JavaScript Function Outliner插件 第三个版本 让你的JavaScript代码也支持折叠...
  4. unity 畸变_unity3d 几种镜头畸变
  5. python 去除读取txt输出时候的换行号
  6. ediplus 复制编辑一列_Excel中如何使用公式查找一列中的重复值并且在另一列里面列出来...
  7. 交换机路由器常用命令
  8. 448.找到所有数组中消失的数字
  9. Solidity编程 五 之 数据类型
  10. Android Gallery3D源码分析(一)
  11. ArcGIS拓扑小技巧:两个面矢量合并但不叠加
  12. 【ESP32 Arduino平衡小车制作】(一)霍尔编码器解码
  13. w7文件加密提示没启用服务器,win7文件夹设置密码没有密钥提示该怎么解决
  14. ASIC和FPGA设计流程
  15. 基于OCCT7.4的实验性CAD程序
  16. ICGC:国际肿瘤基因组协会简介
  17. T-Pot安装教程(保证能运行,附安装需要的所有东西清单)
  18. 通过uart串口和printf函数打印
  19. 思岚科技机器人底盘价格揭秘
  20. STM32F767 资料汇总

热门文章

  1. python零基础怎么学-零基础的小白怎么学python?
  2. 爬虫好学吗python-爬虫Python入门好学吗?学什么?
  3. 网页爬虫 python-python爬虫——爬取网页的中文
  4. 学python需要学数据库吗-学习python用什么数据库好?
  5. python脚本实例手机端-python链接手机用Python实现命令行闹钟脚本实例
  6. python语音在线编辑-Python如何实现文本转语音
  7. 在NVIDIA Jetson Xavier NX上安装llvmlite报错:No such file or directory: ‘llvm-config‘: ‘llvm-config‘
  8. RequestParam注解在required设置为true时失效
  9. java8中Optional的使用
  10. apache2 的https配置和代理https后端nodejs配置