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 ...
随机推荐
- Lazy Load, 延迟加载图片的 jQuery 插件.
Lazy Load 是一个用 JavaScript 编写的 jQuery 插件. 它可以延迟加载长页面中的图片. 在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预 ...
- 关于win7 安装redis的问题
首先在https://github.com/MSOpenTech/redis/releases下载64位的安装包 到任意盘中 将改名为redis 使用cmd命令 启动redis 进入 redis 目录 ...
- SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“
最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...
- redis技巧--IP地址查询对应城市
场景: 根据IP地址判断用户所在地,虽然网上有好多篇了,但我记录一个一看就懂的,不用看超长文字再自己理解了. 我们有城市和IP地址段的对应关系,如: 上海: 202.127.0.0 ~ 202.127 ...
- linux常见命令总结
日志文件说明 /var/log/message 系统启动后的信息和错误日志,是Red Hat最常用的日志之一 /var/log/secure 与系统安全相关的日志信息 /var/log/maillog ...
- iOS 判断网络连接状态的几种方法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #801b80 } p.p2 ...
- node.js
学习网址:http://www.jdon.com/idea/nodejs/
- [整理]AngularJS移动端开发遇到的问题
最近在开发一个移动WebAPP的小项目,因为之前一直使用AngularJS, 对于这个项目,废话不多说,拿过来就用上了. 开发过程是一路通畅和舒服的,但是,却忽略了一个非常重要的问题,移动2G环境下的 ...
- RabbitMQ常用命令行
打印了一些rabbitmq服务状态信息,包括内存,硬盘,和使用erlong的版本信息rabbitmqctl -q status 各个参数说明:http://www.rabbitmq.com/man/r ...
- linux -目录结构
摘自:http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilestruct.html 这个目录结构介绍是我目前看到介绍最全的,有时间在翻译 ...