题目

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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn 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的更多相关文章

  1. 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- ...

  2. 【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 ...

  3. 【Pascal's Triangle II 】cpp

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...

  4. 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 ...

  5. 【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 ...

  6. 【原创】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 ...

  7. 【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 ...

  8. 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 ...

  9. 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- ...

随机推荐

  1. h5 本地存储

    H5本地存储有两个API,一个是Web Storage,还有一个是Web SQL.不管是哪一个,都是基于JavaScript语言来使用,接下来我就教你怎么使用H5本地存储,本文篇幅较大,JS代码较多, ...

  2. java 千分位的添加和去除

    转至:http://blog.sina.com.cn/s/blog_8f99a1640102v1xh.html 将一个数字转换为有千分位的格式: NumberFormat numberFormat1  ...

  3. CentOS下内核TCP参数优化配置详解

    主动关闭的一方在发送最后一个ACK后就会进入TIME_WAIT状态,并停留2MSL(Max Segment LifeTime)时间,这个是TCP/IP必不可少的. TCP/IP的设计者如此设计,主要原 ...

  4. C#之MVC3继续整理问题

    1.注释验证[EmailAddress(ErrorMessage = "×")],用的MVC3框架,此处报错,找不到类“EmailAddress”,看到原文有using Syste ...

  5. hdu-1317 XYZZY---Floyd判连通+bellman最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 题目大意: 题意:有n个房间(n<=100),每个房间有一个点权(第1号房间和第n号房间 ...

  6. 利用kvo实现列表倒计时

    自己稍微记录一下,方便以后用到: 先创建一个定时器的类: #import "TimeCenter.h" @interface TimeCenter () @property (no ...

  7. SPOJ1043 GSS1(线段树)

    题意 给出$n$个数,每次询问区间$(l, r)$内最大字段和 Sol 在合并子树的时候,答案仅有四种情况 打四个标记维护即可 查询同理,用类似update的方式合并 注意查询的时候不能按照以前的方式 ...

  8. 麦子学院python开发全套完整无加密课程

    点击了解更多Python课程>>> 麦子学院python开发全套完整无加密课程 第一阶段:Python基础准备 1.Web前端开发之HTML+CSS基础入门 2.Javascript ...

  9. 如何修改iframe内的页面的元素的样式。。。。

    方法一: 直接通过设置backgroundColor的颜色即可:<!DOCTYPE html><html><head><script>function ...

  10. commons-logging日志实现解耦

    一.需要解耦      日志是实际应用中的一个重要部分,日志系统也有许多开源的实现,如java.util.logging, logback, log4j系列等.      在使用日志系统时,如果与具体 ...