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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn 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 解答的更多相关文章

  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 ...

  2. 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 ...

  3. 【原创】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 ...

  4. 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- ...

  5. 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 ...

  6. 【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 ...

  7. [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 ...

  8. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  9. 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- ...

随机推荐

  1. euctb

  2. 解决selenium 启动ie浏览器报错:Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones

    启动ie代码: System.setProperty("webdriver.ie.driver", "bin/IEDriverServer.exe"); Web ...

  3. python calendar标准库基础学习

    # -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#标准库:日期时间基础学习:calendar:处理日期#例1import calen ...

  4. 讲解版的自动轮播(新手福利)样式和js就不分离了为了看的方便

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. POJ2151Check the difficulty of problems 概率DP

    概率DP,还是有点恶心的哈,这道题目真是绕,问你T个队伍.m个题目.每一个队伍做出哪道题的概率都给了.冠军队伍至少也解除n道题目,全部队伍都要出题,问你概率为多少? 一開始感觉是个二维的,然后推啊推啊 ...

  6. NSString属性什么时候用copy,什么时候用strong?【转】

    转自:http://www.cocoachina.com/ios/20150512/11805.html. 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境): ...

  7. 《第一行代码》学习笔记6-活动Activity(4)

    1.SecondActivity不是主活动,故不需要配置标签里的内容. 2.Intent是Android程序中各组件之间进行交互的一种重要方式,一般可被用于 启动活动,启动服务,以及发送广播等.Int ...

  8. JS如何得到Repeater中TextBox控件的值

    var subsidylCost = document.getElementById("txtSubsidylCost.ClientID").value; 这样获取不到,因为txt ...

  9. 3 linux、windows环境---路径分隔符不同导致的问题

    问题:通常在eclipse,IntelliJ IDEA等进行代码编写时,程序中用到路径通常采用/job/test.properties或D:/job/test.properties等是形式作为文件路径 ...

  10. hdu 畅通工程续

    算法:多源最短路(floyd) 题意:有多个城镇,有些之间有通路,给你起点和终点,输出最短路径: Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路 ...