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->NULL, m = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given m, n satisfy the following condition:
1 ≤ mn ≤ length of list.

解题思路:

指针操作即可,旋转参考Java for LeetCode 025 Reverse Nodes in k-Group JAVA实现如下:

static public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode result = new ListNode(0);
result.next = head;
if (m >= n || m <= 0)
return result.next;
head = result;
for (int i = 0; i < m - 1; i++)
head = head.next;
Stack<Integer> stk = new Stack<Integer>();
ListNode temp = head.next;
for (int i = 0; i <= n - m; i++)
if (temp != null) {
stk.push(temp.val);
temp = temp.next;
}
if (stk.size() == n - m + 1) {
while (!stk.isEmpty()) {
head.next = new ListNode(stk.pop());
head = head.next;
}
head.next = temp;
}
return result.next;
}

Java for LeetCode 092 Reverse Linked List II的更多相关文章

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

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

  3. leetcode -day30 Reverse Linked List II

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

  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]题解(python):092 Reverse Linked List II

    题目来源 https://leetcode.com/problems/reverse-linked-list-ii/ Reverse a linked list from position m to  ...

  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. Java for LeetCode 206 Reverse Linked List

    Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...

  8. 【leetcode】Reverse Linked List II (middle)

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

  9. leetcode: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-> ...

随机推荐

  1. 剑指Offer面试题51(Java版):数组中反复的数字

    题目:在一个长度为n的数组里的全部数字都在0到n-1的范围内. 数组中某些数字是反复的,但不知道有几个数字反复了.也不知道每一个数字反复的次数.请找出数组中随意一个反复的数字. 比如假设输入长度为7的 ...

  2. 如何DIY一个简单的反弹Shell脚本

    00起因 之前在一个服务器上做测试的时候需要从本地连到服务器上,但是服务器没有开ssh服务,但是有python环境,想着自己写一个脚本可以从自己本地连接到服务器,然后服务器端可以将处理完的请求结果返回 ...

  3. C#调用C++Dll封装时遇到的一系列问题【转】

      最近帮底层开发的同时用C#重新封装一下dll,也就是用C#类来封装C++Dll里的方法,以供用户使用. 之前也用到过类似的应用,大多数问题都出在类型转换上,但是这次的应用层出不穷,所以在这里总结一 ...

  4. 查看网络port占用

    Linux和Mac下通用: 1.  利用 netstat 查看网络状态命令: netstat -an|grep port号 2. 利用list open file 命令打开文件(一切都是文件. 包含网 ...

  5. .NET创建宿主设计器--DesignHost、DesignSurface.

    一个窗口在运行时,是这样的: 但是,在设计时,却远比这复杂的多,它需要一个设计器对象:它仅存在于设计时,并连接到运行时存在的对象.   宿主容器 我们可以看到每个窗体和按钮均有与之相关的设计器.这两个 ...

  6. jquery 限制文本框只能输入数字

    $("input[name='fangwenyudinhuishu']").keyup(function(){ var tmptxt=$(this).val(); $(this). ...

  7. 手动脱Mole Box壳实战总结

    作者:Fly2015 这个程序是吾爱破解脱壳练习第8期的加壳程序,该程序的壳是MoleBox V2.6.5壳,这些都是广告,能够直接无视了.前面的博客手动脱Mole Box V2.6.5壳实战中已经给 ...

  8. 又一次认识java(九) ---- 内部类

    注意注意!! ! 前排提示!!.本篇文章过长,最好收藏下来慢慢看.假设你之前对内部类不是非常熟悉,一次性看完,大概你会懵逼. . . 1. 内部类概述 一个类的定义放在还有一个类的内部,这个类就叫做内 ...

  9. 利用MFC里面格式化函数也可以实现可变长度的问题

    直接粘代码: 1: CString str1;  //定义两个MFC里面的CString里面的字符串 2: CString str2; 3: str1.Format("(%d)", ...

  10. python中executemany的使用

    conn = MySQLdb.connect(host = “localhost”, user = “root”, passwd = “password”, db = “myDB”, charset= ...