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. Oracle获取时间日期月份星期数

    1.日期和字符转换函数用法(to_date,to_char)select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; ...

  2. java基础值进制转换

    十进制转换为二进制: 解: 十进制数42连续除以2,当被除数为0时停止除以2,将余数倒加即为结果 :42(10)=101010(2) 注: 计算机内部表示数的字节单位是定长的,且只能是字节(1byte ...

  3. linux远程登录ssh免密码

    原文链接,感谢原作者. (一)问题: 假如我们现在有两台机器:ServerA和ServerB,现在想要让ServerA不用输入密码就能够进行访问. (二)方法和原理: 我们使用ssh-keygen在S ...

  4. hihoCoder挑战赛11 A 随机斐波那契

    算了前三项.....发现是个大水题...   #include<stdio.h> int main() { int n; while (~scanf("%d", &am ...

  5. selenium webdriver学习-怎么等待页面元素加载完成

    http://blog.csdn.net/aerchi/article/details/8055913 WebDriverWait类和ExpectedCondition

  6. 《JS权威指南学习总结--8.8 函数式编程和8.8.1使用函数处理数组》

    内容要点:    和Lisp.Haskell不同,JS并非函数式编程语言,但在JS中可以像操控对象一样操控函数,   也就是说可以在JS中应用函数式编程技术.ES5中的数组方法(诸如map()和red ...

  7. 关于让input=text,checkbox居中的解决方法

    1.type="text"时一般浏览器与IE6在高度上相差2px,并且内容会显示在左上方.解决办法有两种:1.input框的高度不设置,但要设置padding值 2.不设置padd ...

  8. hdu_5193_Go to movies Ⅱ(带插入删除的逆序对,块状链表)

    题目链接:hdu_5193_Go to movies Ⅱ 题意: 有n个人站成一排,每个人的身高为Hi.每次有人加入或者有人离开,就要判断有多少人站反了(i < j&&Hi> ...

  9. hdu_5286_wyh2000 and sequence(分块)

    题目链接:hdu_5286_wyh2000 and sequence 题意: 给一段长度为N的序列,每次询问l-r(l和r和上一次询问的答案有关)内 不同的数的 出现次数的次方 的和.强制在线 题解: ...

  10. C# Monads的实现(二)

    再谈continuation monad 上一篇中我们已经介绍了continuation monad,但是这个monad与Identity,Maybe,IEnumerable monads稍微难于理解 ...