Reverse Linked List II 解答
Question
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.
Solution
Four pointers:
Dummy node, pre, start, then, tmp
First, we find the position to start reversal.
Key point here is to use Dummy node, and pre points the (m - 1) position.
Then, we use three pointers, start, then, tmp to implement reversal. Just the same way as Reverse Linked List.
Several points to note here:
1. Move (m - 1) steps to get pre position
2. Move (n - m) steps to implement reversal
3. Link reversed sub-list with original unreversed sub-lists.
In-place and in one-pass.
/**
* 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 (m == n || head == null)
return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy, start = dummy, then = dummy, tmp = dummy;
// Move pre to (m - 1) position
for (int i = 0; i < (m - 1); i++) {
pre = pre.next;
}
// Start = m
start = pre.next;
then = start.next;
start.next = null;
// Move (n - m) steps for reverse
for (int i = 0; i < (n - m); i++) {
tmp = then.next;
then.next = start;
start = then;
then = tmp;
}
pre.next.next = then;
pre.next = start;
return dummy.next;
}
}
Reverse Linked List II 解答的更多相关文章
- 【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 ...
- 14. 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 -- Reverse Linked List II -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
- 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- ...
- lc面试准备:Reverse Linked List II
1 题目 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 ...
- [Linked List]Reverse Linked List,Reverse Linked List II
一.Reverse Linked List (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- 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- ...
随机推荐
- 二分-poj-3685-Matrix
题目链接: http://poj.org/problem?id=3685 题目大意: 有n*n的矩阵,第i行第j列的数为Aij= i2 + 100000 × i + j2 - 100000 × j + ...
- Android 之窗口小部件详解(三) 部分转载
原文地址:http://blog.csdn.net/iefreer/article/details/4626274. (一) 应用程序窗口小部件App Widgets 应用程序窗口小部件(Widget ...
- myeclipse实现Servlet实例(3) 通过继承HttpServlet接口实现
(1) 在软件公司 90%都是通过该方法开发. //在HttpServlet 中,设计者对post 提交和 get提交分别处理 //回忆 <form action="提交给?&qu ...
- Annotation(四)——Struts2注解开发
Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...
- hdu 2817 A sequence of numbers(快速幂)
Problem Description Xinlv wrote some sequences on the paper a long time ago, they might be arithmeti ...
- JQuery的父、子、兄弟节点查找,节点的子节点循环
Query.parent(expr) //找父元素 jQuery.parents(expr) //找到所有祖先元素,不限于父元素 jQuery.children( ...
- [manacher] hdu 3294 Girls' research
题意: 给一个字符x代表真实的a 然后输出的时候转换 然后就是求最长回文子串的串是什么 长度要大于1 思路: 就是裸的manacher,弄清楚下标的转换关系就好了 代码: #include" ...
- [Redux] Extracting Presentational Components -- Todo, TodoList
Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...
- Linux环境下Eclipse + Tomcat + MySQL 配置J2EE开发环境的方法
1. 版本号信息 (1)CentOS 6.4发行版64位,uname -a 显演示样例如以下: Linux localhost.localdomain 3.11.6 #1 SMP Sat Nov 2 ...
- WebSphere性能优化的几个方法
1.更改http server的配置文件参数KeepAlive. 原因:这个值说明是否保持客户与HTTP SERVER的连接,如果设置为ON,则请求数到达MaxKeepAliveRequest ...