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. Design Pattern——开放封闭原则

    两个特征: 1.对于扩展是开放的 2.对于更改是封闭的 意思就是说:程序在设计的时候,时刻要考虑,尽量让这个类是足够好,写好了就不要去修改了,如果有新的需求来,我们就增加一个类来解决问题,而不要更改原 ...

  2. python连接MongoDB

    1.安装pymongo库 windows下: pip install pymongo 或者 easy_install install pymongo 2.使用pymongo模块连接mongoDB数据库 ...

  3. python3.4项目打包

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

  4. js实例--js滚动条缓慢滚动到顶部

    收集篇(已测)-- <html><head> <script type="text/javascript"> var currentPositi ...

  5. iOS开发极光推送显示 开发证书没有通过验证 是否重新上传证书?解决方法

    1.证书密码错误 2证书环境不匹 3导证书时因手误把私钥导出来了,而不是证书 1.当前上传的p12证书密码输入有误: 2. 证书导出的时候展开了证书,把个人私钥导了出来,导证书的时候请不要展开证书: ...

  6. UITableView简单使用

    在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是UITableView.当然它的广泛使用自然离不 ...

  7. POJ 2368 Buttons(巴什博弈变形)

    题目链接 #include<iostream> #include<cstdio> #include<algorithm> using namespace std; ...

  8. hdu_5968_异或密码(预处理+二分)

    题目链接:hdu_5968_异或密码 题意: 中午,不解释 题解: 前缀处理一下异或值,然后上个二分查找就行了,注意是unsigned long long #include<bits/stdc+ ...

  9. C#获取数字证书

    string Thumbprint = "C2489D912F247C187AA14B1291A6fB612281225D"; X509Store store = new X509 ...

  10. 查看,添加和删除GIT配置的正确姿势

    查看GIT所有配置的命令: git config --list 查看GIT全局配置的命令: git config --global --list 添加GIT全局配置(HTTPS代理) git conf ...