Reverse a singly linked list.

这题因为palindrome linked list 的时候需要就顺便做了一下。利用三个指针:prev, now, next 相互倒腾就行。

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
if (!head) return null;
var prev = null;
var now = head;
var next = head.next while (now) {
now.next = prev;
prev = now;
now = next;
if (next) next = next.next;
}
return prev;
};

还可以递归的解决。算法2:

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseHelp = function(head) {
if (!head.next) return head;
reverseHelp(head.next).next = head;
return head;
} var reverseList = function(head) {
if (!head) return null;
var tail = head;
while (tail.next) {
tail = tail.next;
} reverseHelp(head).next = null;
return tail;
};

但是问题是翻转以后,原来的尾巴变成了头,我没有想到太好的递归方法,就先遍历了一遍找到尾,然后再用递归的翻转head。

第一次提交的时候出现了bug,原因是递归过程 reverseHelp 是返回链表尾,所以最后别忘了把他最终返回的链表尾巴.next = null;

[LeetCode] Reverse Linked List的更多相关文章

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

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

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

  3. 【原创】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-p ...

  4. [leetcode]Reverse Linked List II @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...

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

  6. (leetcode)Reverse Linked List 脑子已经僵住

    Reverse a singly linked list. 参考http://www.2cto.com/kf/201110/106607.html 方法1: 讲每个节点的指针指向前面就可以. /** ...

  7. [Leetcode] Reverse linked list ii 反转链表

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

  8. leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

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

  9. 翻转单链表 leetcode Reverse Linked List

    翻转一个单链表.这个题目听说很多次了,总感觉肯定不是什么难题. 现在真的有点好高骛远了!总感觉那种很难的算法题才是难题,这种题没必要做.其实眼高手低啊. 这种easy题,我都不能一遍ac,这遇到白板编 ...

随机推荐

  1. maven-各配置文件详解

    1.setting.xml http://www.cnblogs.com/yangxia-test/p/4409736.html <?xml version="1.0" en ...

  2. zookeeper启动后没有相关进程

    查看状态报错,报错,百度硕士nc问题,让看.out文件,但是这哥文件是空的,那就看log 016-12-15 14:08:19,355 [myid:] - INFO [main:QuorumPeer$ ...

  3. Python基础7:文件操作

    [ 文件操作] 1 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下: 昨夜寒蛩不住鸣. 惊回千里梦,已三更. 起来独自绕阶行. 人悄悄,帘外月胧 ...

  4. Derivative of the softmax loss function

    Back-propagation in a nerual network with a Softmax classifier, which uses the Softmax function: \[\ ...

  5. linux配置java开发环境

    一.下载jdk java -version 查看有没有安装jdk 下载对应版本的jdk:jdk-java7u60-linux-x64.tar.gz   二.解压 cp jdk-java7u60-lin ...

  6. JQuery datepicker 日期控件设置

    datepicker控件可通过参数设置进行语言切换,以下可实现,系统所有日期控件默认为中文,在特定页面或者特定条件下可切换成英语!~ HTML: <!DOCTYPE html> <h ...

  7. wamp(win1064位家庭版+apache2.4.20+php5.5.37+mysql5.5.50)环境搭建

    wamp环境搭建之软件准备 *php:http://windows.php.net/downloads/releases/php-5.5.37-Win32-VC11-x86.zip *apache:h ...

  8. BerkeleyDB库简介

    BerkeleyDB库简介 BerkeleyDB(简称为BDB)是一种以key-value为结构的嵌入式数据库引擎: 嵌入式:bdb提供了一系列应用程序接口(API),调用这些接口很简单,应用程序和b ...

  9. [译]JavaScript源码转换:非破坏式与再生式

    原文:http://ariya.ofilabs.com/2013/06/javascript-source-transformation-non-destructive-vs-regenerative ...

  10. FusionCharts-堆栈图、xml格式、刷新数据、添加事件link、传参

    *起因* 本来想用Chart.js来搞图表的, 但是来了个新需求,想搞的华丽点,毕竟对Chart.js来说,实现有点难度, *做出的改变* 最终选择了FusionCharts, *难点* 网上关于Fu ...