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.

相对于单纯的链表转置,这个问题需要把链表的一部分做反转。并要求就地反转而且只遍历一次。我的想法是吧链表看成3个部分:list1->list2->list3其中list2代表要反转的部分。我们先找到list2的开始,然后反转list2变为newlist2,然后把链表连接起来list1->newlist2->list3。实现起来还是比较简单的,但是要注意一些边界条件。这些边界条件通过m/n来控制。代码如下:

 public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head==null || head.next==null || m==n || m>n) return head;
ListNode nhead = new ListNode(0);
nhead.next = head;
ListNode start = nhead;
ListNode end = null;
int count = 0;
while(start.next!=null && count<m-1){
count++;
start = start.next;
}
ListNode a = null;
ListNode b = start.next;
ListNode c = null;
end = b;
while(b!=null && count<n){
c = b.next;
b.next = a;
a = b;
b = c;
count++;
}
start.next = a;
end.next = b;
return nhead.next;
}
}

LeetCode OJ 92. Reverse Linked List II的更多相关文章

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

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

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

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

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

  4. LeetCode OJ: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 && 206. Reverse Linked List

    The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ...

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

  7. 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练习题】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. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

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

随机推荐

  1. APK安装时的过滤方式:包名白名单、证书认证

    1.定义一些全局变量,文件位置: Build.java (frameworks\base\core\java\android\os) /** * 包管理方式名称<br> * whiteli ...

  2. Collector

    安装 参考安装文章   创建项目project 转到你创建的目录,运行命令 django-admin.py startproject collector 生成下列文件 collector/ __ini ...

  3. oracle 学习之DG的搭建

    1.配置过程 确认为归档模式 SQL> select log_mode from v$database; LOG_MODE ------------ ARCHIVELOG 配置归档日志存放路径( ...

  4. Dubbo.xml配置源-Dubbo.xsd分析

      我们使用Dubbo时,一般都会使用xml配置基本信息,如项目名称(application).注册中心(register).协议(protocal).服务(service),如下所示: 1 2 3 ...

  5. ios 实现在tableViewCell上面添加长按手势 删除该条cell以及列表后台数据等

    自己的代码  需要   把属性更改成自己要使用的 //创建长按手势 在cellForRowAtIndexPath代理方法中 UILongPressGestureRecognizer *longPres ...

  6. python3.4项目打包

    1.首先下载pyinstaller并且解压(就直接解压再桌面就可以,这样子比较方便) 2.然后就去下载pywin32(按照电脑和python的版本去下载) 我电脑是64位的,python是3.4版本的 ...

  7. MongoDB索引(一)

    原文地址 一.介绍 我们已经很清楚索引会提高查询效率.如果没有索引,MongoDB必须对全部集合进行扫描,即,扫描集合中每条文档以选择那些符合查询条件的文档.对查询来说如果存在合适的索引,则Mongo ...

  8. 清除js-css缓存,清除app缓存,清除php缓存

    入口文件,定义版本常量 define('VERSION','version=002');//自定义版本号 html文件引用常量 <script src="/js/detail.js?& ...

  9. 1、Eclipse的安装

    下载地址:http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigosr1 2.java jdk 的 ...

  10. Java使用千分位并保留两位小数

    double d = 123456.789; DecimalFormat df = new DecimalFormat("#,##0.00"); System.out.printl ...