【Reverse Linked List II】cpp
题目:
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->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.
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
// virtual begin ListNode
ListNode dummy(-);
dummy.next = head;
// move to the m-1 ListNode
ListNode *p = &dummy;
for (int i = ; i < m-; ++i) p = p->next;
ListNode *prev = p;
ListNode *curr = p->next;
for (int i = ; i < n-m; ++i){
ListNode *tmp = curr->next;
curr->next = tmp->next;
ListNode *tmp2 = prev->next;
prev->next = tmp;
tmp->next = tmp2;
}
return dummy.next;
}
};
Tips:
这道题的思路沿用我的这一篇日志:http://www.cnblogs.com/xbf9xbf/p/4212159.html
需要考虑几种case:
m=1的情况
n=end的情况
再submit两次,就OK了。
==================================================
第二次过这道题,大体思路一下子没有完全想起来。想了一下之后,回忆起来了翻转列表类似抽书本的例子。就顺着思路把代码写出来了,一次AC。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* dummpy = new ListNode();
dummpy->next = head;
// find start position
ListNode* start = dummpy;
for ( int i=; i<m-; i++ ) start = start->next;
// reverse list between start and end
ListNode* curr = start->next;
for ( int i=; i<(n-m); ++i )
{
ListNode* tmp = curr->next;
curr->next = tmp->next;
tmp->next = start->next;
start->next = tmp;
}
return dummpy->next;
}
};
【Reverse Linked List II】cpp的更多相关文章
- leetcode 【 Reverse Linked List II 】 python 实现
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- 【Reverse Nodes in k-Group】cpp
题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...
- 【Pascal's Triangle II 】cpp
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 【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 -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
- 【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 ...
- 14. 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 ...
- 92. 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- ...
随机推荐
- logback的configuration
logback的<configuration>只有三个属性: 1.scan[boolean]:当scan被设置为true时,当配置文件发生改变,将会被重新加载.默认值为true. 2.sc ...
- ADO数据库编程详解(C++)----初级入门篇
一.概述 ADO即Microsoft ActiveXData Object,是Microsoft继ODBC之后,基于OLE DB技术的一种数据库操作技术,使您能够编写通过 OLE DB提供者对在数据库 ...
- Android商城开发系列(十三)—— 首页热卖商品布局实现
热卖商品布局效果如下图: 这个布局跟我们上节做的推荐是一样的,也是用LinearLayout和GridView去实现的,新建一个hot_item.xml,代码如下所示: <?xml versio ...
- 基于mllib的协同过滤实战(电影推荐)
//加载需要的包 import org.apache.spark.rdd._ import org.apache.spark.mllib.recommendation.{ALS, Rating, Ma ...
- UVA Live Archive 4490 Help Bubu(状压dp)
难点在于状态设计,从左向右一本书一本书的考虑,每本书的决策有两种拿走或者留下, 对于拿走后的书,之后要放回,但是决策过程中不知道到往哪里放, 虽然前面的书的种类确定,可能是往后面放更优,而后面的书的类 ...
- Aizu 2304 Reverse Roads(无向流)
把有向图修改成无向图,并保证每条边的流量守恒并满足有向容量(即abs(flow(u,v) - flow(v,u)) <= 1)满足限制. 得到最大流,根据残流输出答案. 因为最后少了'\n'而W ...
- 【BZOJ3668】[NOI2014] 起床困难综合症(位运算思想)
点此看题面 大致题意: 给定一些位运算操作,让你在\(0\sim m\)范围内选一个初始值,使其在经过这些运算后得到的结果最大. 前置技能:关于位运算 作为一道位运算的题,如果你不知道什么是位运算,那 ...
- 【洛谷1337】[JSOI2004] 吊打XXX(模拟退火经典题)
点此看题面 大致题意: 一个平面上有\(n\)个点,每个点有1个权值,现在要选择平面上的一个点,使这\(n\)个点的权值乘上到达选定点的距离之和最小. 模拟退火 我们可以用模拟退火来做这道题. 先将\ ...
- 我的Linux学习之路的感悟
首先要跟大家说声抱歉,这么久一直没有更新,有负大家对我的期望. 半年的Linux运维的学习到目前已工作一个月零9天,这一路走来的艰辛和挣扎只有自己最清楚. 首先要感谢公司的同事的宽容接纳和耐心指点.感 ...
- ES6学习(一):数值的扩展
chapter06 数值的扩展 6.1 二进制和八进制 二进制 前缀 0b 或者 0B 八进制 前缀 0o 或者 0O 6.2 Number.isFinite() Number.isNaN() 原先这 ...