题目描述:

解题思路:

  题目大意:给定一个链表,反转第m到第n个结点部分,m、n满足1 ≤ m ≤ n ≤ length of list。

  解题思路参照LeetCode206题,用迭代法,不过要注意以下几点:

  (a):为方便操作,新建一个辅助结点dummy,使其下一个结点指向头节点。

  (b):维护4个指针:pre、current、then、then_next,pre指向第m个结点的前一个结点,current指向当前操作的位于区间[m,n]的结点,then指向current的下一个结点,then_next指向then的下一个结点。

  注意:这里的current、then、then_next就相当于LeetCode206题中的pre、current、next!

  初始化状态如下:

  dummy.next=head;

  pre=dummy;

  current=null,then=null,then_next=null。

  下面以链表1->2->3->4->5,m=2,n=4举例:

  (1)初始状态:

  

  (2)遍历到m位置时:

  (3)一次迭代操作后:

  (4)迭代最终位置:

    (5)执行①pre.next.next=then;②pre.next=current; 后:

Java代码:

 

 //public class LeetCode92为测试
public class LeetCode92 {
public static void main(String[] args) {
ListNode n1=new ListNode(1),n2=new ListNode(2),n3=new ListNode(3),n4=new ListNode(4),n5=new ListNode(5);
n1.next=n2;
n2.next=n3;
n3.next=n4;
n4.next=n5;
System.out.println("原来链表:"+n1.val+"->"+n2.val+"->"+n3.val+"->"+n4.val+"->"+n5.val);
ListNode n=new Solution().reverseBetween(n1,2,4);
System.out.println("反转链表:"+n.val+"->"+n.next.val+"->"+n.next.next.val+"->"+n.next.next.next.val+"->"+n.next.next.next.next.val);
}
}
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode pre=dummy;
ListNode current=head;
for(int i=1;i<=m-1;i++){
pre=current;
current=current.next;
}
ListNode then=null,then_next=null;
if(current!=null)
then=current.next;
if(then!=null)
then_next=then.next;
for(int j=m;j<n;j++){
then.next=current;
current=then;
then=then_next;
if(then_next!=null)
then_next=then_next.next;
}
pre.next.next=then;
pre.next=current;
return dummy.next;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}

程序结果:

【LeetCode92】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】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-> ...

  3. 【链表】 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- ...

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

  5. 【12_206】Reverse Linked List

    本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...

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

  7. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

  8. 【Leetcode】【Easy】Reverse Linked List

    题目: Reverse a singly linked list. 解题: 反转单链表,不再多介绍了. 如果会“先条件->定参数->确定不变式->验证后条件”的思维方法,一定会bug ...

  9. 【LeetCode206】Reverse Linked List★

    题目描述: 解题思路: 关于单链表的反转有迭代和递归两种方法,方法不在多,本文主要介绍迭代的方法. 迭代的方法,要使用三个指针,需要注意一点的是指针的初始化,对第一个指针初始化为pre=null,第二 ...

随机推荐

  1. 分布式配置中心 携程(apollo)

    1.传统配置文件与分布式配置文件区别 传统配置文件:如果修改了配置文件,需要重新打包发布,重新发布服务,而且每个环境的变更配置文件,比较繁琐. 分布式配置文件:将配置文件注册到配置中心上去,可以使用分 ...

  2. Echarts之悬浮框中的数据排序

    Echarts非常强大,配置也非常的多,有很多细节需要深入研究.详解一下关于悬浮框中的数据排序问题 悬浮框的数据排序默认是根据series中的数据位置排序的,在我们想自定义排序时,在echarts的配 ...

  3. 1.print()与input()

    hello world必备->print函数 print(): 作用: 打印函数,打印数据到屏幕中 参数列表: print(value, ..., sep=' ', end='\n', file ...

  4. maven 学习笔记--仓库,聚合和继承,私服搭建

    仓库 http://blog.csdn.net/wanghantong/article/details/36427433 聚合和继承 http://www.cnblogs.com/xdp-gacl/p ...

  5. MySQL 性能监控4大指标——第一部分

    [编者按]本文作者为 John Matson,主要介绍 mysql 性能监控应该关注的4大指标. 第一部分将详细介绍前两个指标: 查询吞吐量与查询执行性能.文章系国内 ITOM 管理平台 OneAPM ...

  6. Ubuntu 16.04 Server 设置静态IP

    一.前言 最近需要在虚拟机当中装个Ubuntu Server 16.04的系统,但是在虚拟机安装的时候,并不像Ubuntu Server 18.04那样能一步步的进行配置,因此导致装好后的虚拟机是动态 ...

  7. Android aapt 工具介绍(转)

    目录 AAPT 工具介绍 AAPT 的帮助信息 查看AAPT的版本 使用AAPT列出资源包apk文件列表 使用AAPT打包资源文件 使用AAPT解压资源包apk   来自:http://mmmyddd ...

  8. SqlServer为字段创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名) 创建索引实例: 聚簇索引 create clustered index index_name on table_name (c ...

  9. 调整 Windows VM 的大小

    本文说明如何使用 Azure Powershell 调整在 Resource Manager 部署模型中创建的 Windows VM 的大小. 创建虚拟机 (VM) 后,可以通过更改 VM 大小来扩展 ...

  10. SQL Server Management Studio记住的密码丢失的问题

    不知道各位经常使用SSMS的时候有没有碰到过这样的烦恼: 记住的密码总是丢失: 步骤如下: 登陆时,选择记住密码 在任何一个存储过程上点击右键,选择修改 这时候再次连接对象资源管理器时,刚刚记住的密码 ...