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. lapis 数据库配置

    备注:  目前支持 postgresql .mysql (实际使用大家可以尝试用下tidb.CockroachDB)   1. pg数据库配置 // config.lua local config = ...

  2. lapis 路由

    1. 路由以及url 模式 参考如下: local lapis = require("lapis") local app = lapis.Application() app:mat ...

  3. Openfire源码使用Install4j打包

    https://www.ej-technologies.com/download/install4j/files 下载并安装install4jhttps://www.ej-technologies.c ...

  4. 基于Oracle的EntityFramework的WEBAPI2的实现(一)——准备工作

    目前在.net的范围内,好的而且方便的ORM的真的不是很多,与VS集成方便的也就当属EntityFramework(以下简称EF,不知道为什么,总EF这个缩写好不专业).但是,好多公司使用的又是ORA ...

  5. Fluent NHibernate AutoMapping Conventions

    前言 使用nhibernate在项目中不管是代码或者xml文件映射方式,如果项目较大编写映射也应该算一笔大的工作量了,使用Fluent Nhibernate自己编写映射规则,将从conventions ...

  6. ElasticSearch读取查询结果(search)

    本文转载自:http://blog.csdn.net/wangxiaotongfan/article/details/46531729?locationNum=6 在es中所有的查询结果都会保存在Se ...

  7. Resource interpreted as Document but transferred with MIME type application/json laravel异常请求返回警告

    一般情况下,laravel在方法里可以向前端返回数组格式 return [], 框架可以自动将数组转成JSON字符串返回,但浏览器会报MIME类型警告, 如是做APP接口可以忽视该警告: 但在前端aj ...

  8. Spring不支持静态注入

    在springframework里,我们不能@Autowired一个静态变量,使之成为一个spring bean,例如下面这样: @Autowired private static YourClass ...

  9. web 服务基础

    用户通过网站访问浏览器都发生了什么 如图,用户请求www.joker.com发生 1. 用户访问网站流程框架2. dns解析原理3. tcp/ip三次握手过程原理4. http协议原理(www服务的请 ...

  10. kafka常用命令(cdh5.10.0+kafka)

    参考资料:http://kafka.apache.org/quickstart 进入kafka安装目录(CDH安装路径为:/opt/cloudera/parcels/KAFKA):进入bin目录: c ...