[LeetCode 206] Reverse Linked List 翻转单链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
return reverseList_iteratively(head);
//return reverseList_recursively(head);//递归方法leetcode超时
}
//迭代
ListNode* reverseList_iteratively(ListNode* head)
{
ListNode* p = nullptr;
ListNode* w = nullptr;
while(head)
{
p = head->next;
head->next = w;
w = head;
head = p;
}
return w;
}
//递归
ListNode* reverseList_recursively(ListNode* head)
{
if(head==nullptr||head->next==nullptr)
{
return head;
}
ListNode* h = reverseList_recursively(head->next);
head->next->next=head;//加入反转后的单链表尾部
head = nullptr;
return h;
}
};
[LeetCode 206] Reverse Linked List 翻转单链表的更多相关文章
- LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)
翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- [LeetCode] 92. Reverse Linked List II 反向链表II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- [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(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- LeetCode 206 Reverse Linked List 解题报告
题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...
- [LeetCode] 92. Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
随机推荐
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- springboot2集成activiti出错
报一个反射错误 java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy 解决方案:http ...
- python+Appium自动化:读取Yaml配置文件
Yaml简介 Yaml:"Yet Another Markup Language"(是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名 ...
- TC做题笔记
SRM593 Div1Medium--May The Best Pet Win(bitset优化) Description 给出n个元素取值的max.min,把这n个元素分割成两个集合,求如何分割使两 ...
- java中commons-beanutils的介绍(转)
1. 概述 commons-beanutil开源库是apache组织的一个基础的开源库.为apache中很多类提供工具方法.学习它是学习其它开源库实现的基础. Commons-beanutil中包 ...
- MultipartFile类
MultipartFile类常用的一些方法: String getContentType()//获取文件MIME类型InputStream getInputStream()//后去文件流String ...
- How To Fix soapUI JVM Maximum Heap Size (-Xmx) Error [Solution]
http://quicksoftwaretesting.com/soapui-jvm-heap-size-xmx-error/ -Xms128m -Xmx1000m -Dsoapui.properti ...
- 阿里云Ubuntu安装Composer和中国镜像
引用: Composer是PHP用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件. PHP ...
- C++ 仿函数和适配器
本文从不断复杂的应用场景入手,来说明C++设计仿函数和适配器的原因,并深入源码来介绍仿函数和适配器的使用方法. 仿函数 现有一个vector,需要统计大于8的元素个数. 使用std::count_if ...
- python中with语句的使用
引言 with 语句是从 Python 2.5 开始引入的一种与异常处理相关的功能(2.5 版本中要通过 from __future__ import with_statement 导入后才可以使用) ...