【LeetCode】206. Reverse Linked List
题目:
Reverse a singly linked list.
提示:
此题不难,可以用迭代或者递归两种方法求解。记得要把原来的链表头的next置为NULL;
代码:
迭代:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) return NULL;
ListNode *pre = head, *cur = head->next;
pre->next = NULL;
ListNode *tmp;
while (cur) {
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
递归:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
return reverseNode(head, NULL);
} ListNode* reverseNode(ListNode* head, ListNode* newHead) {
if (!head) return newHead;
ListNode *next = head->next;
head->next = newHead;
return reverseNode(next, head);
}
};
【LeetCode】206. Reverse Linked List的更多相关文章
- 【LeetCode】206. Reverse Linked List (2 solutions)
		Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ... 
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ... 
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ... 
- 【一天一道LeetCode】#206. Reverse Linked List
		一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ... 
- 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
		The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ... 
- 【leetcode】92. Reverse Linked List II
		Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ... 
- 【easy】206. Reverse Linked List 链表反转
		链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ... 
- 【leetcode】557. Reverse Words in a String III
		Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ... 
- 【LeetCode】151. Reverse Words in a String
		Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ... 
随机推荐
- PHPMailer 命令执行漏洞(CVE-2016-10033)分析(含通用POC)
			对比一下新老版本:https://github.com/PHPMailer/PHPMailer/compare/v5.2.17…master 其实答案呼之欲出了——和Roundcube的RCE类似,m ... 
- Android分享功能实现
			通过系统分享组件实现分享功能 Intent.createChooser() 方法用来弹出系统分享列表. createChooser(Intent target, CharSequence title, ... 
- GreenDao 使用二
			1.创建一个实体类 1 Entity note = schema.addEntity("Note"); 默认表名就是类名,也可以自定义表名 1.dao.setTableName(& ... 
- JQuery控制下拉列表
			//遍历option和添加.移除option function changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE ... 
- Unity中提升像素字体清晰度
			操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Unity5.6.0f3 Unity UI系统是非常好的,但默认情况下,使用像像素艺术风格游戏那样需要非常锋利的边框的字体 ... 
- VUE依赖webpack分别给开发环境和生产环境配置不同的常量值并在项目中动态引用
			当在开发和产品上线的时候,我们经常会遇到在同一个地方由于环境的不同而地址也不同的情况,这时候如果在代码中将该地址写死,那势必会造成上线时手动改动,多人开发及多处使用该地址难以维护等一系列问题,为避免这 ... 
- Hibernate SQLQuery 原生SQL 查询及返回结果集处理-2
			1. 返回List, .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP);将结果转为Map,存放到list中,即list中为若干 ... 
- Android依赖管理与私服搭建
			在Android开发中,一个项目需要依赖许多的库,我们自己写的,第三方的等等,这篇文件介绍的就是自己搭建私服,创建自己的仓库,进行对我们自己写的库依赖管理.本文是在 mac book pro 环境上搭 ... 
- 543. Diameter of Binary Tree
			https://leetcode.com/problems/diameter-of-binary-tree/#/description Given a binary tree, you need to ... 
- weather API 天气api接口 收集整理
			腾讯 http://sou.qq.com/online/get_weather.php?callback=Weather&city=南京 中国天气-weather.com.cn http:// ... 
