Java [Leetcode 206]Reverse Linked List
题目描述:
Reverse a singly linked list.
解题思路:
使用递归或者迭代的方法。
代码如下:
方法一:递归
/**
* 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) { //recursively
return reverseListRecursive(head, null);
}
public ListNode reverseListRecursive(ListNode head, ListNode nextNode){
if(head == null)
return nextNode;
ListNode next = head.next;
head.next = nextNode;
return reverseListRecursive(next, head);
}
}
方法二:迭代
public ListNode reverseList(ListNode head) { // iteratively
ListNode nextNode = null;
while(head != null){
ListNode next = head.next;
head.next = nextNode;
nextNode = head;
head = next;
}
return nextNode;
}
Java [Leetcode 206]Reverse Linked List的更多相关文章
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- [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,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- 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-> ...
随机推荐
- Atmel Studio 6.0新建项目
使用Atmel Studio 6.0新建一个项目 连接Atmel 开发板与调试板,开发板使用的芯片是ATXMGEA128A1 注意: 以上对于开发板与调试板的连接,需要非常注意连接时线的方向, ...
- 关于在js中使用csstext的问题
会将前边所设置的css属性全部清空,慎用!
- java.util.ResourceBundle
转载自: http://lavasoft.blog.51cto.com/62575/184605 这个类提供软件国际化的捷径.通过此类,可以使您所编写的程序可以: 轻松地本地化或翻译 ...
- [转载]Spring Bean Configuration Inheritance
转自: http://www.mkyong.com/spring/spring-bean-configuration-inheritance/ In Spring, the inheritance i ...
- JsRender系列demo(1)-insert-data
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- sun.misc.unsafe类的使用
http://blog.csdn.net/fenglibing/article/details/17138079
- posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
posix 线程(一):线程模型.pthread 系列函数 和 简单多线程服务器端程序 一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属 ...
- linux下安装Apache(https) 服务器证书安装配置指南
一. 安装准备 1. 安装Openssl 要使Apache支持SSL,需要首先安装Openssl支持.推荐下载安装openssl-0.9.8k.tar.gz 下载Openssl:http: ...
- Orcle数据库查询练习复习:一
一.创建数据库和表 drop table student; create table student ( sid int, sname ), sage int, ssex ), snativeplac ...
- Delphi 中的 procedure of object (类方法存在一个隐藏参数self),简单深刻 good
其实要了解这些东西,适当的学些反汇编,WINDOWS内存管理机制,PE结构,看下李维的VCL架构剖析可以很好理解type TMyEvent = procedure of object;这是一种数据类型 ...