LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list.
A linked list can be reversed either iteratively or recursively. Could you implement both?
递归的办法:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null) return null; if(head.next==null)return head; ListNode p=head.next;
ListNode n=reverseList(p); head.next=null;
p.next=head;
return n;
}
}
非递归,迭代的办法:
if(head == null || head.next == null)
return head;
ListNode current = head.next;
head.next = null;
while(current ! = null){
ListNode temp = current.next;
current.next = head;
head = current;
current = temp.next;
}
return head;
LeetCode 206 Reverse a singly linked list.的更多相关文章
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
- Java [Leetcode 206]Reverse Linked List
题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...
- 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. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...
- [LeetCode] 206. Reverse Linked List_Easy tag: Linked List
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. click to show more hints. Subscribe to see which companies asked this ...
随机推荐
- mysql 可以跨库查询
eg: SELECTcity.ID,city.`Name`,city.CountryCode,city.District,city.Population,adv_site.ADD_DATEFROMci ...
- ITShare
框架底层通过IBatis和XML实现ORM映射: 业务处理类似UI,通过Controller和JavaScript交互: 视图通过Castle与NVelocity实现,jquery.tmpl渲染: . ...
- 使用sublime一键格式化XML文件
1 sublime简介 sublime是一款代码编辑和阅读软件,体积小,运行快,界面非常简洁漂亮.官方地址:https://www.sublimetext.com/ 2 在sublime上安装插件 使 ...
- 微软压力测试工具 web application stress
转自 http://www.cnblogs.com/tonykan/p/3514749.html lbimba 铜牌会员 这里给广大的煤油推荐一个web网站压力测试工具.它可以用来模拟多个用户操作网 ...
- maven阿里云中央仓库
配置 修改maven根目录下的conf文件夹中的setting.xml文件,内容如下: <mirrors> <mirror> <id>alimaven</id ...
- Redis安装测试(待完善)
1 Redis安装 在网址http://redis.io/下载redis-3.2.3.tar.gz,解压. 进入解压目录 编译和安装,具体配置项可参考自带的README.md文件 make test ...
- 【转载】跟随 Web 标准探究DOM -- Node 与 Element 的遍历
跟随 Web 标准探究DOM -- Node 与 Element 的遍历 这个是 Joyee 2014年更新的,可能是转战github缘故,一年多没有跟新了.这篇感觉还挺全面,就转载过来,如以前文章一 ...
- 如何在Webstorm/Phpstorm中设置连接FTP,并快速进行文件比较,上传下载,同步等操作
Phpstorm除了能直接打开localhost文件之外,还可以连接FTP,除了完成正常的数据传递任务之外,还可以进行本地文件与服务端文件的异同比较,同一文件自动匹配目录上传,下载,这些功能是平常ID ...
- 10、代码块、构造代码块、静态代码块及main方法之间的关系
1.普通代码块: 在方法或语句中出现在{}之间的类容就称为普通代码块,简称代码块.普通代码块和一般的语句执行顺序由他们在代码中出现的次序决定--“先出现先执行”,即顺序执行. /*下面第一个类时合法的 ...
- 我的vimrc
set nocompatible set langmenu=en_USlet $LANG= 'en_US' source $VIMRUNTIME/vimrc_example.vim source $V ...