LeetCode 206. Reverse Linked List(迭代和递归两种实现)
递归的代码比迭代的代码看起来更清爽一些,也是由于递归对行为进行了抽象吧。
注意到,这是一个尾递归函数。一些编译器会将它优化为迭代,这样一方面,在代码层面保持了清晰的逻辑和可读性。一方面保持了代码的性能。
代码:
class Solution
{
public:
ListNode* reverseList(ListNode* head)
{
// return iteratively(head);
return recursively(nullptr, head);
} private:
ListNode* iteratively(ListNode *head)
{
auto cur = head;
for (ListNode* pre = nullptr; cur != nullptr;)
{
if (cur->next == nullptr)
{
cur->next = pre;
return cur;
} else
{
swap(pre, cur->next);
swap(pre, cur);
}
}
return nullptr;
} // note that this tail recursive algo should be transformed into iterate algo to obtain the better performance
ListNode* recursively(ListNode *pre, ListNode *cur)
{
if (cur == nullptr)
{
return pre;
} else
{
swap(pre, cur->next);
return recursively(cur, pre);
}
}
};
LeetCode 206. Reverse Linked List(迭代和递归两种实现)的更多相关文章
- [LeetCode 206] Reverse Linked List 翻转单链表
本题要求将给定的单链表翻转,是校招面试手撕代码环节的高频题,能很好地考察对单链表这一最简单数据结构的理解:可以使用迭代和递归两种方法对一个给定的单链表进行翻转,具体实现如下: class Soluti ...
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- LeetCode 206. Reverse Linked List(C++)
题目: Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4 ...
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)
翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...
- Java [Leetcode 206]Reverse Linked List
题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
随机推荐
- 使用CXF开发RESTFul服务
相信大家在阅读CXF官方文档(http://cxf.apache.org/docs/index.html)时,总是一知半解.这里向大家推荐一本PacktPub.Apache.CXF.Web.Servi ...
- SQL使用链接服务器执行远程数据库上的存储过程
原文:SQL使用链接服务器执行远程数据库上的存储过程 --创建链接服务器 exec sp_addlinkedserver'server_tmp','','SQLOLEDB','远程服务器名或ip地址' ...
- mtd-utils 及 ubi-utils 交叉编译
参考: http://blog.csdn.net/zjjyliuweijie/article/details/7205374 NAND是嵌入式系统一个很重要的部件,而mtd-utilts中包含了很多针 ...
- ios学习流水账1
1.UIImageview设边框.圆角 需要引QuartzCore/QuartzCore.h> //设UIImageView边框 CALayer *layer = [m_imgView laye ...
- [置顶]
kubernetes资源类型--RC和RS
Replication Controller(RC) RC是K8S中的另一个核心概念,应用托管在K8S后,K8S需要保证应用能够持续运行,这是RC的工作内容. 主要功能 确保pod数量:RC用来管理正 ...
- python的几个概念
1.函数在传递实参的时候是传递的是引用而不是从内存中重新赋相同值给形参. 2.函数名带圆括号和不带圆括号.函数名带圆括号是函数的调用,而函数名代表的是函数体. 3.函数返回值,在函数没有返回值的时候默 ...
- 41个linux命令大全(鸟哥的私房菜)
转http://www.xmws.cn/show-87-419-1.html 41个linux命令大全 发布作者:微思网络 发布时间:2017-01-10 浏览量:709次 学过linux的人 ...
- [Angular] Dynamic component rendering by using *ngComponentOutlet
Let's say you want to rending some component based on condition, for example a Tabs component. Insid ...
- FizzBuzz and Fibonacci优化
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Angular 学习笔记——ng-repeat 隔行换色
<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...