题目描述:

解题思路:

  题目大意:给定一个链表,反转第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. 全局eslint不生效的处理

    react项目里能用上 eslint 的 airbnb 规范真是的,对自己的编码有很好的帮助,不经可以养成良好的代码风格,而且还能检测出 state或者变量 是否 使用过, 然而,所在团队的小伙伴们, ...

  2. 安装cuda8.0时无法安装.net Framework 4.0 错误的解决

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在win7 64位旗舰版(带sp1)上安装cuda时到安装Microsoft.NET Framework4.0,一直停 ...

  3. HCTF2018 pwn题复现

    相关文件位置 https://gitee.com/hac425/blog_data/tree/master/hctf2018 the_end 程序功能为,首先 打印出 libc 的地址, 然后可以允许 ...

  4. 安卓开发实用技巧:TextView预览

    背景: 使用TextView时,为了方便在开发工具中预览效果,需要在TextView中设置文字(如:android:text="Hello World"),但是等到后面提交时,为了 ...

  5. 常用的第三方模块 requests url

    我们已经讲解了Python内置的urllib模块,用于访问网络资源.但是,它用起来比较麻烦,而且,缺少很多实用的高级功能. 更好的方案是使用requests.它是一个Python第三方库,处理URL资 ...

  6. WiFi 统一管理以及设备自动化测试实践

    ATX 安卓设备 WiFi 统一管理以及设备自动化测试实践 (零散知识梳理总结) 此文为转载,感谢作者  目录  众所周知,安卓单台设备的UI自动化测试已经比较完善了,有数不清的自动化框架或者工具.但 ...

  7. [Android] 针对生成的图片文件在系统Gallery不显示的处理

    之前遇到过一个问题,就是发现我在程序中生成一个新的 Bitmap 之后,当我打开系统的 Gallery 查看时,并没有看到新生成的图像.然而打开文件浏览器,找到保存 Bitmap 所在的文件夹下,还能 ...

  8. Django from表单及ajax提交文件

    参考: https://blog.csdn.net/baobao267/article/details/83038323

  9. Innodb页面存储结构-2

    上一篇<Innodb页面存储结构-1>介绍了Innodb页面存储的总体结构,本文会介绍页面的详细内容,主要包括页头.页尾和记录的详细格式. 学习数据结构时都说程序等于数据结构+算法,而在i ...

  10. [POWERSHELL] [.net 3.5] [Windows Server] 在Windows Server上安装.NET3.5

    Install-WindowsFeature Net-Framework-Core -source \\network\share\sxs