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->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]范围内的结点,然后做一次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的更多相关文章
- 【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 ...
- 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 ...
- 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 倒置链表之二
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 反向链表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 【 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- ...
- 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-> ...
- 【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-> ...
随机推荐
- PIX的使用
这几天pix的一个问题可坑死我了,之前用的时候有蓝色的链接,点过去就可以查看相应资源,后来都是黑色的了没有可以点的链接. 看文档翻来翻去也没进展,后来找到了, 先打开render那个窗口 这样even ...
- Linux内存分配----SLAB
动态内存管理 内存管理的目标是提供一种方法,为实现各种目的而在各个用户之间实现内存共享.内存管理方法应该实现以下两个功能: 最小化管理内存所需的时间 最大化用于一般应用的可用内存(最小化管理开销) 内 ...
- Yahoo, Steve blog
Performance Research Domain Sharding revisited A Software Developer's Guide to HTTP How the Browser ...
- 在 Java 中如何更高效地存储和管理 SQL 语句?
[编者按]还在为管理 Java 代码中的 SQL 语句而烦恼吗?让 Zemian 帮你摆脱困境吧!本文系 OneAPM 工程师编译整理 注意:使用java.util.Properties#loadFr ...
- C#索引器及示例
public class IndexSeletor<T> where T:struct { private List<T> _listObj; public IndexSele ...
- POJ1811 Prime Test(miller素数判断&&pollar_rho大数分解)
http://blog.csdn.net/shiyuankongbu/article/details/9202373 发现自己原来的那份模板是有问题的,而且竟然找不出是哪里的问题,所以就用了上面的链接 ...
- POJ 1845 Sumdiv (求某个数的所有正因子的和)
题意: 求A^B的所有正因子的和,最后模9901的结果. 思路: 若对一个数n进行素数分解,n=p1^a1*p2^a2*p3^a3*...*pk^ak那么n的所有正因子之和sum=(1+p1+...+ ...
- System.getProperty()参数
java.version Java 运行时环境版本 java.vendor Java 运行时环境供应商 java.vendor.url Java 供应商的 URL java.home Java 安装目 ...
- Codeforces Round #336 (Div. 2) D. Zuma 区间dp
D. Zuma Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gems ...
- MongoDB (六) MongoDB 集合操作
一. MongoDB 创建集合 createCollection() 方法 MongoDB db.createCollection(name, options) 是用来创建集合. 语法: 基本的 cr ...