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.

 class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null) return head;
ListNode fakehead = new ListNode(0);
fakehead.next = head;
ListNode pre = fakehead; for (int i = 0; i < m-1;i++) pre = pre.next; ListNode start = pre.next;
ListNode then = start.next; for(int i = 0 ;i < n -m ;i++){
start.next= then.next;
then.next = pre.next;
pre.next = then;
then=start.next;
}
return fakehead.next;
}
}

92. Reverse Linked List II(链表部分反转)的更多相关文章

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

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

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

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

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

  6. LeetCode 92. Reverse Linked List II倒置链表2 C++

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

  7. [leetcode]92. Reverse Linked List II反转链表2

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

  8. 92. Reverse Linked List II 翻转链表II

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

  9. [LeetCode]92. Reverse Linked List II反转部分链表

    /* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...

随机推荐

  1. java.lang.OutOfMemoryError 错误分类

    java.lang.OutOfMemoryError: Java heap space原因:Heap内存溢出,意味着Young和Old generation的内存不够.解决:调整java启动参数 -X ...

  2. HTTP 代理

    HTTP 代理: (1) 如果我们一直用同一个IP去爬取同一个网站上的网页,久了之后可能会被该网站服务器屏蔽,因此我们可以使用代理IP来爬取,代理实际上指的就是代理服务器(2) 当我们使用代理IP爬取 ...

  3. linux下用gcc如何生成预处理、汇编等文件

    [gcc -E test.c -o test.i------>预处理文件生成.i 文件.] 1.c语言程序生成过程 C语言程序的生成过程可以简单的分为:编辑.预处理.编译.汇编.链接五个阶断. ...

  4. iPad UIPopoverController弹出窗口的位置和坐标

    本文转载至:http://blog.csdn.net/chang6520/article/details/7921181 TodoViewController *contentViewControll ...

  5. Spring学习笔记--注入Bean属性

    这里通过一个MoonlightPoet类来演示了注入Bean属性property的效果. package com.moonlit.myspring; import java.util.List; im ...

  6. Hadoop学习之路

    Hadoop是谷歌的集群系统的开源实现: -google集群系统:GFS.MapReduce.BigTable -Hadoop主要由HDFS(hadoop distrubuted file syste ...

  7. jQuery子页面获取父页面元素

    $("input[type='checkbox']:checked",window.opener.document);//适用于打开窗口的父页面元素获取 $("input ...

  8. MQTT协议笔记之连接和心跳

    前言 本篇会把连接(CONNECT).心跳(PINGREQ/PINGRESP).确认(CONNACK).断开连接(DISCONNECT)和在一起. CONNECT 像前面所说,MQTT有关字符串部分采 ...

  9. 关于wcf三大工具的使用(wsdl.exe svcutil.exe disco.exe)

    首先,我们必须创建一个wcf服务.并部署到IIS中.这里我已经将一个StudentService服务部署到我自己的电脑了. (1)svcutil.exe svcutil.exe工具的作用是通过服务地址 ...

  10. 170804、使用Joda-Time优雅的处理日期时间

    简介 在Java中处理日期和时间是很常见的需求,基础的工具类就是我们熟悉的Date和Calendar,然而这些工具类的api使用并不是很方便和强大,于是就诞生了Joda-Time这个专门处理日期时间的 ...