Reverse a singly linked list.

Hint:

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.的更多相关文章

  1. C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解

    面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...

  2. LeetCode 206. Reverse Linked List (倒转链表)

    Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...

  3. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  4. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...

  5. Java [Leetcode 206]Reverse Linked List

    题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...

  6. LeetCode 206. Reverse Linked List倒置链表 C++

    Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...

  7. LeetCode 206 Reverse Linked List 解题报告

    题目要求 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5-> ...

  8. [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-> ...

  9. [LeetCode]206. Reverse Linked List(链表反转)

    Reverse a singly linked list. click to show more hints. Subscribe to see which companies asked this ...

随机推荐

  1. Noise Contrastive Estimation

    Notes from Notes on Noise Contrastive Estimation and Negative Sampling one sample: \[x_i \to [y_i^0, ...

  2. SVN 错误 Access to SVN Repository Forbidden的原因及解决方法

    原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/SVN_Access_to_SVN_Repository_Forbidden.html   当我们新 ...

  3. thinkphp模型

    1.获取系统常量信息的方法:在控制器DengLuController里面下写入下面的方法,然后调用该方法. public function test() { //echo "这是测试的&qu ...

  4. thinkphp3.2和phpexcel导入

    先整个最基础的代码,理解了这个,后面的就非常简单了 $file_name= './Upload/excel/123456.xls'; import("Org.Util.PHPExcel&qu ...

  5. jquery 返回顶部 兼容web移动

    返回顶部图片 http://hovertree.com/texiao/mobile/6/tophovertree.gif 具体实现代码 <span id="tophovertree&q ...

  6. C语言中,while()语句中使用赋值语句

    while()语句括号中是一个逻辑表达式,用以判断while循环是否需要继续执行.可以是赋值语句. while循环的一般格式为: while(expr) { ;//body } 其中用来判断循环条件的 ...

  7. AD域控制器通过组策略禁止USB设备

    问题:域环境下如何禁用USB口设备? 第一种:用传统的办法,在Bios中禁用USB. 第二种: 微软技术支持回答:根据您的需求, Windows识别USB设备主要通过两个文件,一个是Usbstor.p ...

  8. 【错误总结】java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor

    运行环境      JDK1.7 64位      Window 7  64位      Ecplise Java EE IDE      Struts 2.3.9      Tomcat 7 说明: ...

  9. 通过代码自定义cell(cell的高度不一致,比如微博)

    1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 (先要调用父控件的nitWithStyle:reuseIdentifie ...

  10. mysql 编译安装

    mysql 编译安装方式:   ```cd /home/oldboy/tools```                创建 目录          if not have   then     mkd ...