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->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.
链接: http://leetcode.com/problems/reverse-linked-list-ii/
题解:
把翻转部分隔离出来,记录这部分之前的节点和之后的节点。然后翻转这部分,再和之前记录的两个节点连接起来就可以了。
Time Complexity - O(n), Space Complexity - O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null || head.next == null || m == n)
return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode preHeadToReverse = dummy, tailToReverse = dummy;
int count = 0; while(count < n) {
if(tailToReverse == null)
return dummy.next;
if(count < m - 1)
preHeadToReverse = preHeadToReverse.next;
tailToReverse = tailToReverse.next;
count++;
} ListNode headToReverse = preHeadToReverse.next, postTailToReverse = tailToReverse.next;
tailToReverse.next = null;
preHeadToReverse.next = reverse(headToReverse);
headToReverse.next = postTailToReverse;
return dummy.next;
} private ListNode reverse(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode dummy = new ListNode(-1); while(head != null) {
ListNode tmp = head.next;
head.next = dummy.next;
dummy.next = head;
head = tmp;
} return dummy.next;
}
}
这道题目应该和其他几道联合起来做,比如按照先后顺序完成以下几道题目
1) Reverse Linked List
2) Reverse Linked List II
3) Swap Node in Pairs
4) Swap Node in K-Gruo
二刷:
我们需要记录四个变量, pre, headToReverse,tailToReverse, postTail,然后当遍历时节点在headToReverse以及tailToReverse的时候我们反转。下面我写得比较麻烦,单独把reverse写成了一个函数,而切要先找到结尾点才能reverse,这样就多遍历了一遍反转区域,还需要好好改写。
Java:
Time Complexity - O(n), Space Complexity - O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || head.next == null || m == n) {
return head;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode preHeadToReverse = dummy, tailToReverse = dummy;
while (m > 1 || n > 0) {
if (m > 1) {
preHeadToReverse = preHeadToReverse.next;
m--;
}
if (n > 0) {
tailToReverse = tailToReverse.next;
n--;
}
}
ListNode headToReverse = preHeadToReverse.next, postTailToReverse = tailToReverse.next;
tailToReverse.next = null;
preHeadToReverse.next = reverse(headToReverse);
headToReverse.next = postTailToReverse;
return dummy.next;
} private ListNode reverse(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1);
ListNode tmp = new ListNode (-1);
while (head != null) {
tmp = head.next;
head.next = dummy.next;
dummy.next = head;
head = tmp;
}
return dummy.next;
}
}
下面直接one pass。 我们先设置fakeHead dummy, 让preHead = dummy, 找的过程中当m > 1的时候 preHeadToReverse = preHeadToReverse .next, 然后找到headToReverse = preHeadToReverse.next,也设置一个标记tail = headToReverse,最后用来添加连接反转前节点n的后一个节点postTail。
接下来我们先设置preHeadToReverse.next = null,在(n > 0以及headToReverse != null)的情况下, 我们利用reverse linkedlist的方法遍历这部分反转区域。遍历完毕以后的headToReverse就等于反转前节点n的下一个节点postTail, 这时候我们只需要将两部分连接起来,做一个 tail.next = headToReverse就可以了。还能再简化。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || head.next == null || m == n) {
return head;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode preHeadToReverse = dummy;
while (m > 1) {
preHeadToReverse = preHeadToReverse.next;
m--;
n--;
}
ListNode headToReverse = preHeadToReverse.next;
ListNode tail = headToReverse, node = headToReverse;
preHeadToReverse.next = null;
while (n > 0 && headToReverse != null) {
node = headToReverse.next;
headToReverse.next = preHeadToReverse.next;
preHeadToReverse.next = headToReverse;
headToReverse = node;
n--;
}
tail.next = headToReverse;
return dummy.next;
}
}
三刷:
方法和二刷一样。
- 先找到m的前一个节点pre,这里要注意是在m > 1的条件下进行遍历
- 设置head = pre.next,用来进行遍历, 设置tail = head,因为翻转完毕以后这个head节点应该处于最后,所以我们设置这个tail用来连接n后面的节点们
- 设置tmp = head,也可以直接设置tmp为null,用来保存时head下一位置的节点
- 设置pre.next = null
- 在n > 0的情况下进行翻转,每次n--
- 最后连接tail和tmp,然后返回结果
Java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy; while (m > 1) {
pre = pre.next;
m--;
n--;
} head = pre.next;
ListNode tail = head;
ListNode tmp = head;
pre.next = null; while (n > 0) {
tmp = head.next;
head.next = pre.next;
pre.next = head;
head = tmp;
n--;
} tail.next = tmp;
return dummy.next;
}
}
Reference:
https://leetcode.com/discuss/10794/share-my-java-code
https://leetcode.com/discuss/25580/simple-java-solution-with-clear-explanation
https://leetcode.com/discuss/35440/240ms-java-solution
https://leetcode.com/discuss/72660/short-java-solution-for-reverse-linked-list-ii
92. Reverse Linked List II的更多相关文章
- 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 ...
- [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 ...
- LeetCode 92. Reverse Linked List II倒置链表2 C++
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
随机推荐
- Windows VC++常见问题汇总
1.warning C4996: 'setmode': The POSIX name for this item is deprecated. Instead, use the ISO C++ con ...
- HOWTO re
\w 字母数字字符 [a-z A-Z 0-9_] \W 非字母数组字符 [^a-z-A-Z 0-9_] \d 十进制数字 [0-9] \D 非数字字符 [^0-9] \s 空白字符 [\t\n\r\f ...
- Linux rar
http://www.vpsyou.com/2010/06/15/to-extract-rar-centos.html wget http://www.rarsoft.com/rar/rarlinux ...
- 常用mysql命令大全
常用的MySQL命令大全 一.连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL. 首先在打开DOS窗口,然后进入目录 mysqlbin,再 ...
- sublime 设置localhost 2
最近sidebar用不了了,提示更新然后就自动卸载了: 研究了下其他方式实现: Sublime Text 2 Sublime Text 3 都可以使用: 菜单 --> Tools --> ...
- 服务器迁移之debian重新配置Web服务的细节
之前配置Linux服务器时采用的是Debian系统一直很稳定,这次准备迁移到新的服务器环境上,好在以前的配置我在博客都做了备忘,所以很容易就搞定了,这次服务系统采用的是最新的Debian 7.0,但是 ...
- CentOS安装,更新Python
1.查看当前Python版本 python -V 2.查看当前CentOS版本 cat /etc/redhat-release 3.安装所有的开发工具包 yum groupinstall " ...
- oracle 绿色版本 instantclient 使用说明
1,将instantclient直接放到D盘根目录 2,注册表修改 3,点击工具>>首选项, 在Oracle主目录名中输入“D:\instantclient_10_2\”,在OCI库中输入 ...
- java小提示:标示符常见命名规则、常用ASCII
标示符常见命名规则: A:包:全部小写B:类或者接口:首字母大写:StudentC:方法或者接口:首字母小写,第二个单词开始开始,每个单词首字母大写:studentAgeD:常量:全部大写,多个单词之 ...
- 【递推】BZOJ 3930: [CQOI2015]选数
Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选出的N个整数都求一次最大公 ...