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.

解答思路:记录[m, n]范围内的结点,然后做一次reverse。

/**
* 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) {
vector<ListNode*> range(n - m + 1); ListNode* iter = head;
for(int i = 1; i < m; ++i)
iter = iter->next; for(int i = m, j = 0; i <= n; ++i, ++j)
{
range[j] = iter;
iter = iter->next;
} for(size_t i = 0; i < range.size() / 2; ++i)
swap(range[i]->val, range[range.size() - i - 1]->val); return head;
}
};

  

leetcode:Reverse Linked List II的更多相关文章

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

  2. lc面试准备:Reverse Linked List II

    1 题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1 ...

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

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

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

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

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

  8. LeetCode OJ: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-> ...

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

随机推荐

  1. razor GPU

    抓之前要把设置里面 Setting -Debug Setting -Graphic -PA DEBUG yes -RAZOR GPU yes replay是个很有用的功能,要设置 -Debug Set ...

  2. Redis Master/Slave 实践

    本次我们将模拟 Master(1) + Slave(4) 的场景,并通过ASP.NET WEB API进行数据的提交及查询,监控 Redis Master/Slave 数据分发情况,只大致概述,不会按 ...

  3. NVelocity 在.Net的三种用法

    NVelocity 使用文件型模板例子 using NVelocity; using NVelocity.App; using NVelocity.Runtime; VelocityEngine vl ...

  4. SQL注入测试用例

    //看看是什么权限的and 1=(Select IS_MEMBER('db_owner'))And char(124)%2BCast(IS_MEMBER('db_owner') as varchar( ...

  5. JS获取节点方法

    1. 通过顶层document节点获取:(1) document.getElementById(elementId):该方法通过节点的ID,可以准确获得需要的元素,是比较简单快捷的方法.如果页面上含有 ...

  6. Unity3D 运营商支付 安卓打包的陷阱 libunicomsdk.jar

    原地址:http://blog.csdn.net/alking_sun/article/details/36624491 想想做Unity3D SDK集成已经快2年了,遇到过不少很棘手的SDK,其中以 ...

  7. iOS应用间的跳转和传值

    在第一个应用程序中info.plist设置 URL Identifier: 该字符串是你自定义的 URL scheme 的名字 注意: URL Schemes 是一个数组,允许应用定义多个 URL s ...

  8. UITableView多选删除

    设置一个在编辑状态下点击可改变图片的cell FileItemTableCell.h #import <UIKit/UIKit.h> @interface FileItemTableCel ...

  9. Webpack教程二

    Webpack教程一 开发技巧 启用source-map 现在的代码是合并以后的代码,不利于排错和定位,只需要在config中添加 ... devtool: 'eval-source-map', .. ...

  10. Good Bye 2015 A. New Year and Days 签到

    A. New Year and Days   Today is Wednesday, the third day of the week. What's more interesting is tha ...