LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list.
题目标签:Linked List
题目给了我们一个链表,要求我们倒转链表。
利用递归,新设一个newHead = null,每一轮 把下一个 node 存入 next 记住,把目前的点 cursor 连到 newHead;
然后把 next 当作新的 cursor;把 cursor 当作新的 newHead 递归下去。
换句话说,每到一个点,把下一个点(右边的)记住,传到下一轮,然后把目前的点反向链接(向左链接)。
Java Solution:
Runtime beats 22.53%
完成日期:06/11/2017
关键词:singly-linked list
关键点:递归向左链接;记住右边的点代入下一轮
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution
{
public ListNode reverseList(ListNode head)
{
return dfs(head, null);
} private ListNode dfs(ListNode cursor, ListNode newHead)
{
if(cursor == null)
return newHead; ListNode next = cursor.next;
cursor.next = newHead; return dfs(next, cursor);
}
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 206. Reverse Linked List (倒转链表)的更多相关文章
- [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 ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- 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 a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...
- 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-& ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- 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. click to show more hints. Subscribe to see which companies asked this ...
随机推荐
- postgresql遇到的性能问题
问题SQL scwksmlcls.wk_cls_c , scwklrgcls.wk_lrg_cls_nm , scwkmdlcls.wk_mdl_cls_nm , scwksmlcls.wk_sml_ ...
- HDU_2079_(01背包)(dfs)
选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- ThinkPHP---thinkphp实用项
[一]代码调试 (1)跟踪信息 ①简介:用于展示系统执行的相关状况,类似于快递的物流信息.ThinkPHP中默认关闭.如需使用,则通过配置项SHOW_PAGE_TRACE(显示页面跟踪)来配置. ②位 ...
- 【原】Python学习
1.常用模块介绍 #python -m SimpleHTTPServer 执行上面的命令就会在服务器当前目录下启动一个文件下载服务器,默认打开8000端口.这个时候,你只需要将IP和端口告诉客户端,即 ...
- 浅析Oracle中的不等于号
前几天碰到一个关于Oracle不等于的问题,最后搜索了一下,发现下面资料,拿来跟大家分享一下,需要的朋友可以参考下 关于Oracle中的不等于号: 在Oracle中, <> != ...
- background 背景类八大属性
background 背景类八大属性 背景颜色(当同时定义了背景颜色和背景图像时,背景图像覆盖在背景颜色之上) background-image:背景图像 background-repeat:背景图像 ...
- NOIP 前的垂死挣扎
计划每天十题吧,可能会一天水题一天难题吧.题目以杂题为主,没有专题可言. 10.11 计划: [x] P2939 [USACO09FEB] 改造路 Revamping Trails [ ] P3601 ...
- Springboot2.0中jpa默认创建的mysql表为myisam引擎问题
使用Springboot2.0后,使用jpa操作mysql数据库时,默认创建的表的引擎是myisam,myisam是不能加外键的,找了一些资源,最终可以用此方法解决! yml格式: spring: j ...
- vue v-model 的注意点
在使用表单元素 input 的 v-model 指令时,碰到一个问题: 如上图,修改 input 的内容,以便随时显示:但显示时明显慢一步. <template> <div> ...
- 使用Mybatis的逆向工程自动生成代码
1.逆向工程的作用 Mybatis 官方提供了逆向工程,可以针对数据库表自动生成Mybatis执行所需要的代码(包括mapper.xml.Mapper.java.pojo). 2.逆向工程的使用方法 ...