Java [Leetcode 206]Reverse Linked List
2015-12-28 13:24
501 查看
题目描述:
Reverse a singly 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学习资料
- javaweb 解决请求相应的乱码问题
- Java this关键字简单理解
- 【从头开始学Java】Java的继承(枚举、反射)
- 日常开发遇到的问题解决办法-java
- 简单springmvc实现
- 深入分析JavaWeb Item34 -- DBUtils框架学习
- Maven以及其Eclipse插件m2eclipse的安装
- 聊聊高并发(三十八)解析java.util.concurrent各个组件(十四) 理解Executor接口的设计
- springmvc注解开发-高级之数据回显
- springmvc注解开发-高级之Validation
- Java代理系列-动态代理
- The Java Programming Language4th读书笔记-第七章 语言符号、值和变量
- java 插入排序
- Java [Leetcode 191]Number of 1 Bits
- Spring中注入的理解(SSH框架)
- SSH框架中在Spring容器配置bean的a…
- 关于java中天花板函数Math.ceil()&…
- Java EE。ajax 验证用户名
- Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别