【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 ...
随机推荐
- eclipse下使用hibernate tools实现hibernate逆向工程
一 安装hibernate tools插件 1 在线安装 通过Eclipse的Help->Install New Software 在线安装插件,插件连接为: eclipse helios(3 ...
- 2013第51周二eclipse启动优化
2013第51周二eclipse启动优化 今天注意到了eclipse.ini配置文件中gc.log--在eclipse启动时清空,然后记录了eclipse每次运行过程中的gc分配情况,看到了一篇很好的 ...
- FTA
FTA - 维基百科,自由的百科全书 FTA 维基百科,自由的百科全书 跳转至: 导航. 搜索 FTA可以指: 自由贸易协定(Free Trade Agreement) 自由贸易区(Free Tr ...
- 在Eclipse中用SWT设计界面
摘自http://www.tulaoshi.com/n/20160129/1488574.html 在Eclipse中用SWT设计界面 1. 为什么要使用SWT? SWT是一个IBM开发的跨平台GU ...
- VM虚拟机下CentOS 6.5配置IP地址的三种方法
1.自动获取IP地址 虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址. #dhclient 自动获取ip地址命令 #ifconfig 查询系统里网卡信息,i ...
- Maven引入hadoop依赖包出错解决办法
错误: ArtifactTransferException: Failure to transfer org.apache.hadoop:hadoop-hdfs:jar:2.6.0 from http ...
- bootstrap之Flick
Flick package io.appium.android.bootstrap.handler; import com.android.uiautomator.core.UiDevice; imp ...
- python 笔记3--高级特性
切片 语法 L[l:r] 取L[l],L[l+1]-L[r-2],L[r-1] L[l:r:m] 取L[l],L[l+m],L[l+2*m],L[l+3*m]-.(满足l+n*m<=r-1) t ...
- 【set&&sstream||floyed判环算法】【UVa 11549】Calculator Conundrum
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bored ...
- android 硬件解码学习
FileInputStream in = new FileInputStream("/sdcard/sample.ts"); String mimeType = "vid ...