*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 ...
随机推荐
- PHPCMS中load_model,load_app_class, load_sys_func
phpcms v9 二次开发: 在一个项目开发中遇到需要二次开发,但我们需要了解load_model,load_app_class, load_sys_func的含义: 1.调用数据库模型 //从”p ...
- 【研究】struts2-045漏洞
攻击者可以通过构造HTTP请求头中的Content-Type值可能造成远程代码执行. 工具: K8(链接:https://pan.baidu.com/s/1kVxgFNx 密码:ygxf) Tomca ...
- (转)博弈 SG函数
此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------- ...
- RNN(recurrent neural network)学习笔记
参考:https://www.jianshu.com/p/9dc9f41f0b29 以及<白话深度学习与TensorFlow> 与前馈神经网络.卷积神经网络等不同之处在于,RNN具有一定 ...
- ngin日志格式
日志格式 为了更好满足分析场景,推荐采用如下log_format配置. log_format main '$remote_addr - $remote_user [$time_local] &qu ...
- PIE SDK打开网络地图数据
1. 数据介绍 网络地图数据是在线地图服务发布出来的数据,其支持数据的网络查看和传输,极大的促进了GIS的发展. 目前PIE SDK支持百度地图.谷歌地图.高德地图.天地图.Bing地图.ArcGIS ...
- Linux 运维之硬链接与软链接详解
了解这个的时候不如先知道下文件吧. 我们知道文件都有文件名与数据,但是呢这个在 Linux 上被分成两个部分:用户数据 (user data) 与元数据 (metadata). 用户数据,即文件数据块 ...
- python读取excel表格生成sql语句 第一版
由于单位设计数据库表·,都用sql.不知道什么原因不用 powerdesign或者ermaster工具,建表很痛苦 作为程序猿当然要想办法解决,用Python写一个程序解决 需要用到 xlrd li ...
- TOJ 4003 Next Permutation
描述 It is an interesting exercise to write a program to print out all permutations of 1, 2, …, n. How ...
- java的访问修饰符
Java中通过访问控制符(default,private,public,protected)来控制对类.变量.方法.构造方法的访问. 下表说明了4中修饰符的访问权限: 修饰符 当前类 同一包内 子孙类 ...