Java for LeetCode 092 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->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 ≤ m ≤ n ≤ 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的更多相关文章
- [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-> ...
- 【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 ...
- 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- ...
- [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 ...
- [LeetCode]题解(python):092 Reverse Linked List II
题目来源 https://leetcode.com/problems/reverse-linked-list-ii/ Reverse a linked list from position m to ...
- 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-> ...
- Java for LeetCode 206 Reverse Linked List
Reverse a singly linked list. 解题思路: 用Stack实现,JAVA实现如下: public ListNode reverseList(ListNode head) { ...
- 【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-> ...
- 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-> ...
随机推荐
- concurrencyProgrammingGuide 1
thread用来表述执行代码的独立path.os x的线程执行基于POSIX 线程API. process用来表述一个运行操作,可以包含多个线程. task用来描述工作的抽象概念. Concurren ...
- 【java】Map、Set、List不同数据结构的各种不同循环迭代的效率对比,使用场景
Map.Set.List不同数据结构的各种不同循环迭代的效率对比,使用场景 引申一个地址:Map迭代的使用keySet和entitySet的效率
- excel导出 jxl.jar包
导入jxl.jar包, 代码如下: package com.gree; import java.io.IOException; import java.io.OutputStream; import ...
- java.lang.UnsupportedClassVersionError: Unsupported major.minor version 49.0的错误 [转]
一:要解决的问题 我们在尝鲜 JDK1.5 的时候,相信不少人遇到过 Unsupported major.minor version 49.0 错误,当时定会茫然不知所措.因为刚开始那会儿,网上与此相 ...
- Enumerate Combination C(k, n) in a bitset
Suppose n<=32, we can enumerate C(k, n), with bits representing absence or presence, in the follo ...
- (最短路径算法整理)dijkstra、floyd、bellman-ford、spfa算法模板的整理与介绍
这一篇博客以一些OJ上的题目为载体.整理一下最短路径算法.会陆续的更新... 一.多源最短路算法--floyd算法 floyd算法主要用于求随意两点间的最短路径.也成最短最短路径问题. 核心代码: / ...
- JAVA进阶-多线程(2)
堵塞队列: 1)BlockingQueue该接口提供了: add()/remove() 假设当队列没有数据,从队列中取数据;或者队列中数据已满, 向队列中加入数据;则会抛出异常. put()/take ...
- 【Python基础】之异常
一.常见异常 try: open('abc.txt','r') except FileNotFoundError: print('异常啦!') 输出结果: ======= 异常啦! 我们通过 open ...
- chessy 提高篇系列 阅读笔记
java提高篇(一)—–理解java的三大特性之封装 封装的好处, 汇聚属性和方法 减少修改对 其他处的影响 控制get和set方法. java提高篇(二)—–理解java的三大特性之继承 继承的好处 ...
- 多媒体开发之---live555 分析客户端
live555的客服端流程:建立任务计划对象--建立环境对象--处理用户输入的参数(RTSP地址)--创建RTSPClient实例--发出DESCRIBE--发出SETUP--发出PLAY--进入Lo ...