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

题目意思:

翻转给定区间[m,n]的链表。

第一个节点m=1。

1<=m<=n<=length。

解题思路:

就我目前学习到的,有用指向指针的指针的,有插入,有逆转的。但是所有方法的核心,都其实是一个算法,就是利用3个指针修改区间内的节点的next值,且要保证已修改的链表是可以被找到的,即可以连入原链表中。

假设有一个链表有1,2,3,4,5,6,6个节点。m=2,n=5。

我们添加一个dummy节点,方便操作第一个节点。

首先让pre指向第m个节点前面那个,cur指向第m个节点,p1指向m的下一个节点,因为p1有可能是NULL,所以当p1不是NULL的时候,p2指向p1的下一个节点。

上图画出了这几个指针指向情况的开始状态和我们希望的终止状态。

在最终状态下,再通过其中方框中的两步就可以我们需要的链表了。

而cur,p1,p2三个指针在区间内向前移动并且将p1的next指向cur的过程则包含在一个for循环内部。如下:

代码如下:

 class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode *dummy = new ListNode();
dummy->next = head; ListNode *pre = dummy, *cur = head;
for(int i = ; i < m; i++){
pre = cur;
cur = cur->next;
} ListNode *p1,*p2;
if(cur)
p1 = cur->next;
if(p1)
p2 = p1->next;
for(int j = m; j < n; j++){
p1->next = cur;
cur = p1;
p1 = p2;
if(p2) p2 = p2->next;
}
pre->next->next = p1;
pre->next = cur; return dummy->next;
}
};

【LeetCode练习题】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. 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- ...

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

  4. Java for LeetCode 092 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. [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 ...

  6. 【leetcode】Reverse Linked List II (middle)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

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

  8. leetcode: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-> ...

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

  10. [leetcode]92. Reverse Linked List II反转链表2

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

随机推荐

  1. redis的安装与配置

    官网 http://redis.io/download 管理工具 http://docs.redisdesktop.com/en/latest/quick-start/ https://redisde ...

  2. xsd转实体类

    话说VS自带的工具,可以将xsd或者xml格式的文件转成实体类,大概格式如下 使用VS2005工具XSD.exe(SDK/v2.0/Bin/xsd.exe)自动生成实体类: xsd /c /names ...

  3. SqlServer 数据库日志无法收缩处理过程

    今天按常用方法收缩一个测试用的数据库日志,发现没法收缩! dbcc sqlperf(logspace)     USE [dbname] GO ALTER DATABASE [dbname] SET  ...

  4. [Node.js]在windows下不得不防的小错误

    TypeError: Arguments to path.join must be strings at f (path.js:204:15) at Object.filter (native) at ...

  5. android XML解析之DOM解析方式

    DOM 解析方式步骤: 第一步:首选需要获得DOM解析器工厂实例 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ...

  6. Windows7中安装内存与可用内存不一致的解决办法

    转载:http://blog.sina.com.cn/s/blog_56741a420100h9d1.html 问题现象: 安装完Windows7后,在计算机->属性中,会看到安装内存.但有时在 ...

  7. 疯狂Android第二章:Adapter以及部分控件使用

    第二章 重点:1.理解View以及各种布局的优缺点,适用场景. 2.熟练掌握adapter原理与用法. 3.熟悉其它控件的基本使用方法. /////////////////////////////// ...

  8. 11g导入大量包含子分区的数据时表空间不足

    问题描述: ORACLE11g使用impdp数据泵导入时遭遇: ORA-01691: Lob 段 ISCS.SYS_LOB0000100750C00045$$ 无法通过 128 (在表空间 RT_DA ...

  9. Oracle闪回详解

      1.问题定义 闪回是dba做的工作.现在也可授权给某个用户. 闪回的定义:就是将用户错误的操作回恢到以前的状态.即使你的事务提交的commit. 如果你删除了一个表.Drop table(DDL) ...

  10. C# 中的常用正则表达式大全

       这是从网上找来的,收藏一下备用,用到之处可以节省不少时间哦! 只能输入数字: "^[0-9]*$" . 只能输入n位的数字:"^\d{n}$". 只能输入 ...