*92. Reverse Linked List II (follow up questions)
Reverse a linked list from position m to n. Do it in one-pass and in-place
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
FIrstly, I wanna use stack but need cosume space
Finally, I use in-plcae method
思路:reference: http://bangbingsyb.blogspot.com/2014/11/leetcode-reverse-linked-list-ii.html
In code
1. dfine dummy node
2. find the start node
3. reverse the node
4. connect the list
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode d = new ListNode(0);
d.next = head;
//find start point(head )
ListNode pre = d;
for(int i = 0; i<m-1; i++){
pre = pre.next;
head = head.next;
}
ListNode tailReverse = head;
ListNode end = null, temp;
for(int i = 0; i<n-m+1; i++){ // need +1
temp = head.next;
head.next = end;
end = head;
head = temp;
}
//System.out.println(end.val);
pre.next = end;
tailReverse.next = head;
return d.next;
}
}
*92. Reverse Linked List II (follow up questions)的更多相关文章
- 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 ...
- 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- ...
- [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] 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】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 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-> ...
- LeetCode OJ 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-> ...
- 【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-> ...
- 【一天一道LeetCode】#92. Reverse Linked List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...
随机推荐
- n个骰子的和,组成数字m的可能
//n个骰子的和,组成数字m的可能 function f(n,m) { if(n==1){ return 1; } var len=m-n; var sum=0; while (len>=0){ ...
- centos7.4 上面 docker 启动 cAdvisor 报错问题解决
2018-10-29 docker 启动 cAdvisor,操作系统CentOS7: docker run \ --volume=/:/rootfs:ro \ --volume=/var/run:/v ...
- java 简单的des加密示例
1.加密结果 包含 : 对int加密 .对string加密.对byte[]加密. 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/Ciph ...
- 转 禁用HAIP,cluster_interconnects配错了集群无法启动
简介: 在Oracle 11.2.0.2之前,私网的冗余一般是通过在OS上做网卡绑定(如Bond等)来实现的,从Oracle 11.2.0.2版本开始推出HAIP(Highly Available V ...
- phpstorm一些简单配置
1.字体大小和行间距 2.设置编码:包括编辑工具编码和项目编码
- windows使用bat文件定时备份文件
遇到一个需求,需要备份Access数据库,Access生成的数据都保存在xx.mdb文件中,所以考虑使用windows任务 定时执行一个备份文件的bat文件来解决这个问题. backup.bat文件代 ...
- 07.使用FileStream类来实现对大文件的复制
namespace _20.使用FileStream类来实现多媒体文件的复制 { class Program { static void Main(string[] args) { //需要被复制的文 ...
- python学习(六)--正则的一些例子
import re #正则表达式#compile函数,--将正则表达式转变为内部函数,提高执行效率strr = "python123456"pattern = "Pyth ...
- XML再深入
XML 命名空间 XML 命名空间提供避免元素命名冲突的方法. 使用前缀来避免命名冲突 在 XML 中的命名冲突可以通过使用名称前缀从而容易地避免. 该 XML 携带某个 HTML 表格和某件家具的信 ...
- IO流之缓冲流
缓冲流 Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度 缓冲流,根据流的分类字节缓冲流与字符缓冲流. 字节缓冲流 字节缓冲流根据流的方向,共有2个 l 写入数据到流中,字节缓冲输出流 ...