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. Start Developing iOS Apps Today

    view types - view常见类型

  2. ylbtech-czgfh(规范化)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-czgfh(规范化)-数据库设计 DatabaseName:czgfh(财政规范化) Model:账户模块.系统时间设计模块.上报自评和审 ...

  3. GIS可视化——热点图

    一.简介 SuperMap iClient for JavaScript提供了热点图(HeatMapLayer),用于渲染数据衰减趋势.颜色渐变的效果. 原理:在客户端直接渲染的栅格图,热点图的渲染需 ...

  4. 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之静态页面

    一.引言 接着上一篇,京东个人中心的所有功能数据分析完成之后,现在需要把静态页面完成,实现过程中要用到的技术有:Bootstrap.html5表单新特性等.除此之外,还要利用Node.js的Expre ...

  5. centos 7 mariadb 安装

    yum install -y mariadb mariadb-server systemctl start mariadb systemctl enable mariadb #初始化 mysql_se ...

  6. Git相关命令教程

    一.在GitHub上创建新项目 (1)在GitHub首页 “New repository”,创建新版本库“test” (2)在本地使用GitBash,将repository clone到本地 git ...

  7. Linux系统编程_1_文件夹读取(实现简单ls命令)

    闲来无事.随便写写,实现简单的ls命令: | 1 #include <stdio.h> | 2 #include <stdlib.h> | 3 #include <dir ...

  8. 七款做好DevOps的强大工具

    原文链接: 7 cool tools for doing devops right 传统把开发和运营割裂开的做法,实则不适合现代产品和服务开发的需求,如今把开发和运营作为整体来看待的DevOps工程思 ...

  9. PHP通过prepare执行查询取得数据

    可以用来防止sql注入 <?php $pdo=new PDO("mysql:host=localhost;dbname=itest", 'root',''); //先构建查询 ...

  10. 用buildroot qemu 执行 Android 系统

    准备 qemu. 编译 arm 的执行环境 $ wget http://wiki.qemu-project.org/download/qemu-2.0.0.tar.bz2 $ tar xzvf qem ...