Question

Reverse a singly linked list.

Solution 1 -- Iterative

Remember to set head.next = null or it will report "memory limit exceeds" error.

 /**
* 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) {
ListNode current = head, prev = head, post = head;
if (current == null || current.next == null)
return current;
current = current.next;
head.next = null;
while (current.next != null) {
post = current.next;
current.next = prev;
prev = current;
current = post;
}
current.next = prev;
return current;
}
}

Solution 2 -- Recursive

We can also use recursion to solve this problem.

 /**
* 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 || head.next == null)
return head;
ListNode second = head.next;
head.next = null;
ListNode newHead = reverseList(second);
second.next = head;
return newHead;
}
}

Reverse Linked List 解答的更多相关文章

  1. Reverse Linked List II 解答

    Question Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Giv ...

  2. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  3. [LeetCode] Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  4. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  5. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  6. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  7. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

  8. 14. Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  9. 【12_206】Reverse Linked List

    本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...

随机推荐

  1. JS 点击复制Copy插件--Zero Clipboard

    写博客就是一周工作中遇到哪些问题,一个优点就是能够进行一个总结,另外一个优点就是下次遇到相同的问题即使那你记不住,也能够翻看你的博客攻克了.相同也能够帮到别人遇到与你一样问题的人.或者别人有比你更好的 ...

  2. UITabBar背景、icon图标颜色、被选中背景设置以及隐藏UITabBar的两种方式

    一.对UITabBar背景和icon图标的一些设置 (1)因为直接给UITabBar设置的背景颜色显示的不纯,半透明的感觉,所以,有时候我们可以直接利用纯色的图片作为背景达到想要的效果: (2)给ic ...

  3. html5 video播放不全屏

    <video controls="controls" webkit-playsinline src="${page.videoUrl }" type=&q ...

  4. .net中用到的一些方法

    //文件操作string fullDirPath = Utils.GetMapPath(string.Format("/aspx/{0}/", buildPath)); Direc ...

  5. 《JavaScript 闯关记》之原型及原型链

    原型链是一种机制,指的是 JavaScript 每个对象都有一个内置的 __proto__ 属性指向创建它的构造函数的 prototype(原型)属性.原型链的作用是为了实现对象的继承,要理解原型链, ...

  6. 如何在asp.net中如何在线播放各类视频文件

    一.后台拼字符串动态加载写法 前台调用代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

  7. nginx配置文件(反向代理+集群+动静分离)

    1.nginx纯反向代理配置(nginx.conf): #user nobody;worker_processes 4;error_log logs/error.log info;pid logs/n ...

  8. 01 日志组件XLog

    本文地址为:http://www.cnblogs.com/ADTL/p/5357259.html XLog为XCode的日志组件,为系统基本功能. 使用示例: 1.新建WinForm程序 2.引用Ne ...

  9. Django 实战 之 搭项目(正在更新)

    系统:win10 python版本:python 3.5 工具: pyCharm 3.4 professional 源码来源:https://github.com/ouzhigang/django-o ...

  10. vs2010中自动实现抽象方法

    由于刚接触vs,感官上虽然和eclipse差不多,但是一些快捷都不太相同,导致一开始使用时候非常不习惯. 不过刚开始嘛,写点相当小白的东西,也没有用到太多功能,也就暂时忽视,用的时候再说. 但是今天, ...