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

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} m
* @param {number} n
* @return {ListNode}
*/
var reverseBetween = function(head, m, n) {
var prev = null;
var now = head;
var next = head.next;
var iprev = null;
var inext = null;
var ihead = null;
var itail = null;
var s = 1; var newhead = false; while (now) {
if (s >= m && s <= n) {
if (s == m) {
itail = now;
iprev = prev;
}
if (s == n) {
ihead = now;
inext = next;
}
now.next = prev;
prev = now;
now = next;
if (now) {
next = now.next;
} if (s == n) {
if (!iprev) {
head = ihead;
} else {
iprev.next = ihead;
}
itail.next = inext;
break;
}
} else {
prev = now;
now = next;
if (now) {
next = now.next;
}
}
s++;
}
return head;
};

代码比较丑,用了大量的零时变量来存东西,思路跟翻转整个链表那题如出一辙。简述一下思路就是:用三指针prev, now, next 遍历链表,当到达指定范围后开始翻转操作,并顺便记录翻转那部分的头和尾(ihead, itail)还有相应接头部分的指针(iprev, inext)以便最后翻转完了以后把两部分接起来。特别注意的是链表的头可能会被改变。

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

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

  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 @ Python

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

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

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

  6. LeetCode Reverse Linked List II 反置链表2

    题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...

  7. lc面试准备:Reverse Linked List II

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

  8. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  9. LeetCode 92. 反转链表 II(Reverse Linked List II)

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

随机推荐

  1. git-添加公钥

    安装完git后,打开开git bush 输入以下命令 git config --global user.name "Your Name" git config --global u ...

  2. github-windows本地安装

    Step1 准备工作 msysgit,下载地址为 http://msysgit.github.io/ . Eclipse IDE for Java EE Developers(必须是这个,自带Egit ...

  3. MinGW安装c-c++

    1.安装环境 2.添加环境变量 3.运行终端

  4. SQL0946N错误及DB2事务日志

    在对DB2数据库进行批量增删的时候, 如果数据量比较大会导致SQL0964N错误, DB2 Knowledge center(http://pic.dhe.ibm.com/infocenter/db2 ...

  5. Beta阶段第四次Scrum Meeting

    情况简述 Beta阶段第四次Scrum Meeting 敏捷开发起始时间 2016/12/13 24:00 敏捷开发终止时间 2016/12/14 24:00 会议基本内容摘要 进度平稳推进,分配新任 ...

  6. 解决Spring MVC @ResponseBody返回中文字符串乱码问题

    spring mvc使用的默认处理字符串编码为ISO-8859-1 解决方法: 第一种方法: 对于需要返回字符串的方法添加注解,如下: @RequestMapping(value="/use ...

  7. <<< eclipse软件部署修改项目的访问地址

    在eclipse开发javaweb项目的时候,访问项目时需要在浏览器地址输入:localhost:8080/项目名  但是大多数部署到服务器的时候访问的是根目录,就是不加localhost:8080后 ...

  8. Google Map API V3开发(6) 代码

    Google Map API V3开发(1) Google Map API V3开发(2) Google Map API V3开发(3) Google Map API V3开发(4) Google M ...

  9. ssh模仿ansible批量执行命令的功能

    #!/bin/bash ssh_hosts=("IP" "IP1".......) user=root remote_cmd="df -h" ...

  10. java8--类加载机制与反射(java疯狂讲义3复习笔记)

    本章重点介绍java.lang.reflect包下的接口和类 当程序使用某个类时,如果该类还没有被加载到内存中,那么系统会通过加载,连接,初始化三个步骤来对该类进行初始化. 类的加载时指将类的clas ...