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. 四、基于hadoop的nginx访问日志分析---top 10 request

    代码: # cat top_10_request.py #!/usr/bin/env python # coding=utf-8 from mrjob.job import MRJob from mr ...

  2. 测试开发面试-java持续累积

    接口和抽象类的区别 对java线程的理解 对java并发的理解 webservice的特点,用webservice的原因 守护线程和非守护线程 单例的实现,单例并发 如何实现定义一个类,只实现接口的任 ...

  3. 搭建NFS服务器

    1:yum install -y nfs-utils-* portmap-* 2:NFS安装完毕,需要创建共享目录,共享目录在vi /etc/exports文件里面配置,可配置参数如下: /data/ ...

  4. MySQL查看已安装的编译参数

    MySQL5.1.x版本 cat $path/bin/mysqlbug|grep configure MySQL5.5.x

  5. 换行的css属性

    //正常换行  word-break:keep-all;word-wrap:normal; //下面这行是自动换行  word-break:break-all;word-wrap:break-word ...

  6. 顶级域名和二级域名cookie共享删除和修改

    原文地址: https://segmentfault.com/a/1190000006932934

  7. ruby 基础知识(一)

    突然今天发现一大神的博客:http://www.cnblogs.com/jackluo/archive/2013/01/22/2871655.html    相信初学者会受益颇多 ruby  参考文档 ...

  8. 移动开发框架剖析(二) Hammer专业的手势控制

    浏览器底层并没有给元素提供类似,单击,双击,滑动,拖动这些直接可以用的控制接口,一切的手势动作都只能通过模拟出来.移动端浏览器唯一给我们提供的就只是mousedown -> mousemove ...

  9. 我们平时是怎么写html和css的?

    文章的起因,我只是为了回复一个帖子,http://bbs.csdn.net/topics/390908928?page=1 结果,一扯就根本停不下来.索性,一捅为快,反正是周末. 拿到效果图时,有这么 ...

  10. SQL语句生成指定范围内随机数

    1.生成随机实型数据 create procedure awf_RandDouble @min dec(14,2), @max dec(14,2), @result dec(14,2) output ...