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. Storyboards Tutorial 04

    设计好后运行发现没有任何变化,是空白的.这是因为你的tableview相关的delegate方法还在.所以首先要屏蔽或者删除在PlayerDetailsViewController.m 如下的操作 # ...

  2. [深入浅出iOS库]之数据库 sqlite

    一,sqlite 简介 前面写了一篇博文讲如何在 C# 中使用 ADO 访问各种数据库,在移动开发和嵌入式领域也有一个轻量级的开源关系型数据库-sqlite.它的特点是零配置(无需服务器),单磁盘文件 ...

  3. Android 进度条对话框ProgressDialog

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  4. 一入python深似海--range()、list与for

    range使用方法 使用python的人都知道range()函数非常方便,今天再用到他的时候发现了非常多曾经看到过可是忘记的细节. 这里记录一下: range(1,5)#代表从1到5(不包括5) [1 ...

  5. poor-pigs(非常好的思路)

    https://leetcode.com/problems/poor-pigs/ package com.company; class Solution { // 下面第二种做法貌似是OJ原来的做法, ...

  6. GPS整数。度分秒转换

    例如30.453280 104.2018怎么把度数转换为度分秒的格式要详细换算方法 例如30.453280°,30.453280°,则有30°0.453280°×60= 27.1968′则有27′0. ...

  7. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  8. jquery:选择器 过滤器

    容易理解错误的地方: 1.假如我们想要让一个表格中第八列的所有单元格,都隐藏起来.我们可能会这么写$("table tr td:eq(8)").css("display& ...

  9. SharePoint 的PowerShell命令之获取所有网站模版

    Get-SPWebTemplate | select Name, Title

  10. hdu2141Can you find it?

     给你四个集合.要你从这四个集合中 各取出一个数出来,推断,取出的前三个数的和 是否等于第四个数. 数据比較大.我的做法是将 前两个集合全部数全部和的情况取出来, 然后二分查找第四个集合和第三集合 ...