【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.
For example:
Given1->2->3->4->5->NULL, m = 2 and n = 4,return
1->4->3->2->5->NULL.Note:
Given m, n 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的更多相关文章
- 【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 -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- ...
- [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-> ...
- 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-> ...
- [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 ...
- 【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-> ...
- 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-> ...
- 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 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 ...
- [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 ...
随机推荐
- 一个C#多线程的工作队列
多线程添加元素到队列中,队列根据绑定 的事件进行自动处理,可以设置WorkSequential属性来实现对队列处理的单线程(严格顺序处理)或者多线程处理(循序出队,但是 多线程处理,不保证对队列元素的 ...
- Linux系统编程(1)——文件与I/O之C标准I/O函数与系统调用I/O
Linux系统的I/O也就是一般所说的低级I/O--操作系统提供的基本IO服务,与os绑定,特定于Linux平台.而标准I/O是ANSI C建立的一个标准I/O模型,是一个标准函数包和stdio.h头 ...
- 学DSP(二):目标芯片28335,GO!
28335开发板有了,之前没有用过TI的片子,还是先看看这个东西是啥东西. 进入28335的中文网页: http://www.ti.com.cn/product/cn/tms320f28335 ...
- Asp.NET MVC 技术参考:http://kb.cnblogs.com/zt/mvc/
Asp.NET MVC 技术参考:http://kb.cnblogs.com/zt/mvc/
- 百度地图LV1.5实践项目开发工具类bmap.util.jsV1.2
/** * 百度地图使用工具类-v1.5 * * @author boonya * @date 2013-7-7 * @address Chengdu,Sichuan,China * @email b ...
- OpenWrt 学习网址
http://m.blog.csdn.net/blog/woods2001/8137755
- openstack API debug OpenstackEveryProject_CLI,curl_based
1,基于Openstack 每个服务组件client客户端,eg,nova 客户端软件包名称是python-novaclient, 别的都一样,把python-novaclient (nova替换成组 ...
- jsp用jstl标签比较枚举
日向博客最近在优化,有这一样一个小问题,我希望在下面的消息中心页面,未读的消息链接显示蓝色,已读的消息显示红色: 这就需要用jstl做一个判断. 之前的代码是这种形式: 消息中心:<br> ...
- Unity 制作RPG地图2(自己控制地图上图标)
上一次用Unity摄像机方式实现了地图的制作,现在介绍另一种实现地图的方式: 自己通过代码实现小地图NCP图标的显示和隐藏 制作地图的步骤: 1. 根据游戏人物的3D坐标转换成2D平面坐标,根据距离显 ...
- IBatis——(一)
IBatis是持久层的框架,也就是我们说的Dao层框架,关注数据库操作以及和Java对象之间的关联,我们将这样的框架也称之为ORM(Object/Relaction Mapping)框架.而这里映射的 ...