递归的代码比迭代的代码看起来更清爽一些,也是由于递归对行为进行了抽象吧。

注意到,这是一个尾递归函数。一些编译器会将它优化为迭代,这样一方面,在代码层面保持了清晰的逻辑和可读性。一方面保持了代码的性能。

代码:

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(迭代和递归两种实现)的更多相关文章

  1. [LeetCode 206] Reverse Linked List 翻转单链表

    本题要求将给定的单链表翻转,是校招面试手撕代码环节的高频题,能很好地考察对单链表这一最简单数据结构的理解:可以使用迭代和递归两种方法对一个给定的单链表进行翻转,具体实现如下: class Soluti ...

  2. leetcode 206. Reverse Linked List(剑指offer16)、

    206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...

  3. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  4. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  5. LeetCode 206. Reverse Linked List(C++)

    题目: Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4 ...

  6. [LeetCode] 206. Reverse Linked List ☆(反转链表)

    Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3-> ...

  7. LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)

    翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...

  8. Java [Leetcode 206]Reverse Linked List

    题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...

  9. C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解

    面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...

随机推荐

  1. shell 查找目录

    查找目录:find /(查找范围) -name '查找关键字' -type d

  2. JD2

    Business Requirement Support l Develops and communicates plan to manage vendor review of requirement ...

  3. intel 硬盘加速技术

    Intel Smart Response Technology 混合硬盘技术 Intel Rapid Storage Technology SERVER:

  4. ansible的inventory文件含义

    默认文件为/etc/ansible/hosts 例如 [test] web.yinzhipeng.com dhcp ansible_ssh_host=172.16.18.195 1.中括号中的名字代表 ...

  5. appium Parameters were incorrect

    raise exception_class(value) selenium.common.exceptions.WebDriverException: Message: Parameters were ...

  6. apache 限制某些目录不能访问通过rewrite实现

    通过deny allow访问控制肯定是可以实现的单个目录,但是这个必须指定准确的目录,如果有很多个目录,但是都包含某个名字,比如bbs.1.com/1/tmp/123.htmlbbs.1.com/2/ ...

  7. Eclipse下的java工程目录

    对新手来讲,一个Java工程内部的多个文件夹经常会让大家困惑.更可恶的是莫名其妙的路径问题,在Eclipse编写Java程序中,出现频率最高的错误很可能就是路径问题. 这些问题原因其实都是一个,就是关 ...

  8. 为集群配置Impala和Mapreduce

    FROM: http://www.importnew.com/5881.html -- 扫描加关注,微信号: importnew -- 原文链接: Cloudera 翻译: ImportNew.com ...

  9. 【重点突破】—— 百度地图在React单页面应用中的使用

    前言:百度地图是网页中使用地图的常用第三方工具,这里结合React项目中学到的应用场景总结一些使用要点. 一.在网页中嵌入百度地图 搜百度地图开放平台,注册百度开发者账号 控制台:查看应用.创建应用( ...

  10. ECSHOP生成缩略图模糊

    原因是因为ECSHOP生成缩略图时,用到的函数 imagejpeg()  没有设置质量参数.注释:质量参数为可选项,范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大).如果没有设置质量参 ...