【LeetCode92】Reverse Linked List II★★
题目描述:

解题思路:
题目大意:给定一个链表,反转第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★★的更多相关文章
- 【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 ...
- 【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-> ...
- 【链表】 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- ...
- 【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 ...
- 【12_206】Reverse Linked List
本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...
- 【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-> ...
- 【leetcode】Reverse Linked List(easy)
Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...
- 【Leetcode】【Easy】Reverse Linked List
题目: Reverse a singly linked list. 解题: 反转单链表,不再多介绍了. 如果会“先条件->定参数->确定不变式->验证后条件”的思维方法,一定会bug ...
- 【LeetCode206】Reverse Linked List★
题目描述: 解题思路: 关于单链表的反转有迭代和递归两种方法,方法不在多,本文主要介绍迭代的方法. 迭代的方法,要使用三个指针,需要注意一点的是指针的初始化,对第一个指针初始化为pre=null,第二 ...
随机推荐
- video视频在本地可以播放,在服务器上不可以播放
今天遇到一个比较坑的问题,视频在本地可以播放,然后放到服务器上面就播放不了,原因是因为服务器上面不支持mp4的播放,下面看解决办法.1.首先进入IIS(Internet Information Ser ...
- springboot No Java compiler available for configuration options compilerClassName: [null] and compil
今天使用eclipse创建springboot整合jsp出现一个问题,在idea中并没有遇到这个问题.最后发现是需要在eclipse中添加一个eclipse依赖,依赖如下: <dependenc ...
- 关于子元素的margin-top溢出和元素浮动对父元素高度影响解决方案
以下是个人学习笔记,仅供学习参考. 1.关于子元素的margin-top作用在无margin-top-border的父元素上导致子元素的margin-top溢出问题. 在给没有margin-top-b ...
- 你真的理解PeopleSoft的Web概要(web profile)嘛
Web概要通过配置门户相关属性来控制门户的所有行为. 在PS系统中可以创建多个web概要,你可以通过不同的web概要来让用户路由到一个特定的web概要来控制超时,外观,缓存设置等.例如,通过Peopl ...
- CentOS7上Python3.5安装
CentOS7上Python3.5安装 1.下载 https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz 2.上传到服务器 3. yum in ...
- tilestache
pip install tilestache -i https://mirrors.ustc.edu.cn/pypi/web/simple python C:/Python27/ArcGIS10.4/ ...
- php实现头像预览上传功能
最近在做php第二阶段的项目,需要用到头像上传的功能 我们要完成头像上传功能,一共要写两个php页面,第一个页面我们叫做touxiang.php,第二个页面我们叫做upload.php 1.touxi ...
- 有关 Android Studio 重复引入包的问题和解决方案
虽然相同包名相同类名的文件在不同 SDK 中出现的概率极低,但是一旦出现,处理起来就比较棘手.最好的解决方案就是联系提供 SDK 的技术人员反映问题,让其通过修改源码重新打包一个新的 Jar 包. 还 ...
- Java Web工程搭建方法
搭建一个简单的Web工程主要是以下几步: 一.下载所需工具 ①java ②eclipse ③tomcat 注意:java与eclipse版本不匹配(32位或者64位),会导致eclipse启动时 ...
- iOS 开发之环形倒计时进度条(虚线/实线)
代码很简单,一看便知.这里为顺时针,若想要逆时针,clockwise改为0,还需更改起始角度和终点角度. 源码地址:https://github.com/LfyDragon/CountDown 直接上 ...