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

Note: 1 ≤ m ≤ n ≤ length of list.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL

FIrstly, I wanna use stack but need cosume space

Finally, I use in-plcae method

思路:reference: http://bangbingsyb.blogspot.com/2014/11/leetcode-reverse-linked-list-ii.html

 
反转整个链表的变种,指定了起点和终点。由于m=1时会变动头节点,所以加入一个dummy头节点
 
1. 找到原链表中第m-1个节点start:反转后的部分将接回改节点后。
从dummy开始移动m-1步
 
D->1->2->3->4->5->NULL
       |
      st
 
2. 将从p = start->next开始,长度为L = n-m+1的部分链表反转。
            __________
            |                  |
            |                 V
D->1->2<-3<-4    5->NULL             
       |     |           | 
      st    p          h0         
 
3. 最后接回
 
            __________
            |                  |
            |                 V
D->1   2<-3<-4    5->NULL             
       |________|                

In code

1. dfine dummy node

2. find the start node

3. reverse the node

4. connect the list

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode d = new ListNode(0);
d.next = head;
//find start point(head )
ListNode pre = d;
for(int i = 0; i<m-1; i++){
pre = pre.next;
head = head.next;
}
ListNode tailReverse = head;
ListNode end = null, temp;
for(int i = 0; i<n-m+1; i++){ // need +1
temp = head.next;
head.next = end;
end = head;
head = temp;
}
//System.out.println(end.val);
pre.next = end;
tailReverse.next = head;
return d.next;
}
}

*92. Reverse Linked List II (follow up questions)的更多相关文章

  1. 92. Reverse Linked List II【Medium】

    92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...

  2. 92. 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] 92. Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  4. [LeetCode] 92. Reverse Linked List II 反向链表II

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

  5. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  6. leetcode 92 Reverse Linked List II ----- java

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

  7. LeetCode OJ 92. 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-> ...

  8. 【leetcode】92. 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-> ...

  9. 【一天一道LeetCode】#92. Reverse Linked List II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...

随机推荐

  1. SQL注入(过滤空格和--+等注释符)

    1.地址:http://ctf5.shiyanbar.com/web/index_2.php(过滤了空格和--+等注释符) 思路:确定注入参数值类型,直接输入单引号,根据报错信息确定参数值类型为字符型 ...

  2. 【C#】隐式类型var

    在.NET 3.0后微软引入了隐式类型var,编译器可以自动判断变量的类型,通过var这个隐式类型,可以提高开发人员的开发效率,很多时候可以不考虑对象的类型,编译器会自动帮我们判断 使用隐式类型和使用 ...

  3. innosetup的静默安装与卸载

    静默安装,就是减少程序与用户的交互,一站式的安装过程(一气呵成) 1. 静默安装参数 innosetup的静默安装是通过参数来控制的 1.1.  /silent                     ...

  4. this,super,和继承

    this是指当前对象的引用,super是指直接父类的引用 比如 我建造一个类 public class Person(){ private String name; private  int age; ...

  5. Eclipse error: “The import XXX cannot be resolved”

    解决 Eclipse error: “The import XXX cannot be resolved” eclipse中修改: 1. 项目-->Properties-->java bu ...

  6. HDU 1754——I Hate It——————【线段树单点替换、区间求最大值】

    I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  7. nyoj1087——摆格子——————【规律题】

    摆方格 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 给你一个n*n的方格,每个方格里的数必须连续摆放如 1 2 4 3 ,下图为不连续的,请输出从左上角到右下角的 ...

  8. [转]微信小程序(应用号)是什么,是否值得投入进来做?

    本文转自:http://www.woshipm.com/it/417887.html 距离张小龙的那场首次公开演讲已经有九个月了,而在那场演讲中备受关注的「应用号」在千呼万唤中终于以「小程序」的名字正 ...

  9. JavaScript实现StringBuffer

    function StringBuffer() { this._strings = new Array(); } StringBuffer.prototype.Append = function(_s ...

  10. 译:面试投行的20个Java问题

    原文链接:https://dzone.com/articles/var-work-in-progress 作者:Anghel Leonard 译者:沈歌 如果你需要准备面试,可以看一下这篇博客中20个 ...