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. Sqlite基础简介

    1.什么是SQLite ? -> SQLite简介 SQLite 是一个进程内的库,是一种轻量级的.自给自足的.无服务器的.无需配置的,事务性的SQL数据库引擎.和他其他的数据库一样,SQLit ...

  2. protobuf接口调用报错:java.nio.charset.MalformedInputException: Input length = 1

    使用protobuf定义的接口api发起http请求报错,日志如下: [-- ::] DEBUG AbstractPool: - server updated, node=, server={ nod ...

  3. 移动端固定头部和固定左边第一列的实现方案(Vue中实现demo)

    最近移动端做一份报表,需要左右滚动时,固定左边部分:上下滚动时,固定头部部分. 代码在Vue中简单实现 主要思路是: a.左边部分滚动,实时修改右边部分的滚动条高度 b.头部和内容部分都设置固定高度, ...

  4. oracle 卸载操作

    1. 用 oracle 用户登录 如果要再次安装, 最好先做一些备份工作. 包括用户的登录脚本,数据库自动启动关闭的脚本,和 Listener 自动启动的脚本. 要是有可能连创建数据库的脚本也保存下来 ...

  5. ipvsadm的命令参考

    相信很多同学和我差不多,半桶水,貌似在配置lvs双机的时候,直接用的keepalived,ipvsadm就用来看看,感觉没啥用,今天无聊到处逛发现,某大神说,keepalived只是ipvsadm的一 ...

  6. Eclipse中添加web dynamic project【菜鸟学JAVA】

    很多eclipse版本是不能直接新建web dynamic project的,需要从网上找插件或更新.我的Eclipse的版本是(Version: 3.7.0) 比较方便的是在Help → Insta ...

  7. webpack快速入门(三):资源管理

    上一章说了基本的webpack是用,包括命令行打包,npm脚本打包等基础的东西. 这篇说一下webpack的资源管理,包括(图片,字体,数据),首先调整一下项目结构成: webpack-demo |- ...

  8. python学习 (三十四) Python文件操作

    1 写文件 my_list = ["] my_file = open("myfile.txt", "w") for item in my_list: ...

  9. ALSA声卡笔记1---ALSA驱动框架

    1.声卡驱动程序sound.c (1)入口函数里通过register_chrdev()函数注册file_operations 结构体 (2)file_operations 结构体,里面只有open函数 ...

  10. toString()和toLocaleString()的区别

    在数字转换成字符串的时候,并没有感觉这两个方法有什么区别,如下: 1 2 3 4 5 6 7 8 var e=123     e.toString() "123"   e.toLo ...