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 ≤ mn ≤ 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) {
ListNode* fakeHead = new ListNode();
fakeHead->next = head; //find the last element of first section
ListNode* rHead = fakeHead;
int i = ;
for(; i < m-; i++){
rHead = rHead->next;
} //m-n element will be reversed
ListNode* secondHead = rHead->next; //head of the original second section
ListNode* current = secondHead->next; //current node to reverse
ListNode* tmp;
for(i = m; i < n; i++){ //reverse from m to n
tmp = rHead->next;
rHead->next = current;
secondHead->next = current->next;
current->next = tmp;
current = secondHead->next;
} //keep the third section and return
return fakeHead->next; }
};

92. Reverse Linked List II (List)的更多相关文章

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

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

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

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

  5. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

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

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

  8. 【leetcode】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-> ...

  9. 【一天一道LeetCode】#92. Reverse Linked List II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...

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

随机推荐

  1. gradle asciidoc 使用

    备注:    次文档参考github 例子   1.环境准备 node npm (yarn) java KindleGen 备注: 具体的安装可以参考网上相关文档,KindleGen 下载地址:htt ...

  2. kong 安装

    1. yum 参考信息 https://bintray.com/kong/kong-community-edition-rpm $ sudo yum install epel-release $ su ...

  3. spss v21.0 使用笔记

    spss v21.0 使用笔记 有问题,戳官方帮助文档 神经网络 分析-神经网络-多层感知机 变量. 分析-神经网络-多层感知机-变量 预测变量可指定为因子(分类)或协变量(刻度). 在因变量框输入预 ...

  4. Error unmarshalling file:/opt/test/jboss/server/defalt/conf/bootstrap.xml

    启动命令:#/usr/local/jboss/bin/run.sh -b 0.0.0.0 -c defalt 启动的defalt写错了,应该写default.

  5. Nexus搭建私服

    什么是Nexus Nexus是一个强大的Maven仓库管理器,它极大地简化了本地内部仓库的维护和外部仓库的访问. 运行原理 本地仓库与私服处在同一个局域网中,当本地仓库没有资源时,会向私服发起请求获取 ...

  6. C# Socket Post File

    ///<summary> ///向服务器发送混合型的请求,1:成功发送,0:发送失败 ///</summary> ///<param name="paranam ...

  7. 全局获取Context

    1.定制一个Application类,管理全局的状态信息 public class MyApplication extends Application{ private static Context ...

  8. 让html标签显示在页面上

    用 <xmp></xmp> 标签包起来,里面的所有文字会原样显示出来 <xmp><b>1</b><div>2</div&g ...

  9. Centos6-7安装Python3.5

    可以看到我们现在是2.7.5的,现在我安装一个3.5版本的 安装python3之前首先安装ssl开发库,否则会造成python3的ssl库都无法使用!!! yum install openssl op ...

  10. 使用 Windows 运行时中异步性来始终保持应用程序能够快速流畅地运行

    转自:http://blogs.msdn.com/b/windowsappdev_cn/archive/2012/03/26/windows.aspx 人类的思维方式在本质上不是同步的,这直接影响着我 ...