/**
* 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 * res=head;
if(m==n) return res; int i=;
ListNode * pre=NULL; //记录待反转链表的前驱,注意,带反转链表从头开始的情况,没有前驱
ListNode * left; //记录待反转链表的开始
if(m>) {
pre=head;
while(i<m-){
pre=pre->next;
i++;
}
left = pre->next;
}
else left = head; i=;
ListNode *post=left; //记录待反转链表的后继,注意赋初值为left,而不是head,pre之类的
while(i<n-m+){
post=post->next;
i++;
} ListNode *right=reverselist(left,n-m+); //记录待反转链表的结束 if(pre) pre->next =right;
else res = right; while(right->next){
right=right->next;
} right->next=post;
return res;
} //把head开始长度为l的拉链,反转
ListNode* reverselist(ListNode* head,int l)
{
ListNode * pre=NULL;
ListNode * cur=head;
ListNode * post=NULL;
int i=; while(i<l){
post = cur->next;
cur->next = pre;
pre = cur;
cur = post;
i++;
}
return pre;
}
};
  

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

  3. 【原创】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-p ...

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

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

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

  7. [Linked List]Reverse Linked List,Reverse Linked List II

    一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...

  8. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

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

  10. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

随机推荐

  1. Flask 与 Celery 在 windows 下的集成问题

    Flask 与 Celery 在 windows 下的集成问题 所有的 Web 框架内部的视图中不适合执行需要长时间运行的任务,包括 Flask .Django 等.这类型的任务会阻塞 Web 的响应 ...

  2. yum报错: Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

    在Centos 5.x或6.x上安装RHEL EPEL Repo repository,资源库,源的意思.RHEL EPEL(Extra Packages for Enterprise Linux)  ...

  3. form和validate示例

    //验证from表单 $(function () { $("#addUserForm").validate({ rules: { txtName: { required: true ...

  4. .Net内存优化的几点经验

    以前从来没有想过.Net开发居然存在内存无法释放的问题,总是认为GC给我处理好了一切.现在GIS二次开发结合三维球开发,没有想到存在如此严重的内存增长,很快内存就不够用了,导致系统各种不稳定.球体和三 ...

  5. nsarray排序

    // //  main.m //  05-数组排序 // //  Created by apple on 14-3-21. //  Copyright (c) 2014年 apple. All rig ...

  6. C# WPF定时器

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的学习是避免被 ...

  7. Hadoop2.2.0 第一步完成MapReduce wordcount计算文本数量

    1.完成Hadoop2.2.0单机版环境搭建之后需要利用一个例子程序来检验hadoop2 的mapreduce的功能 //启动hdfs和yarn sbin/start-dfs.sh sbin/star ...

  8. 不等高cell搭建(二)

      一.commentView模块搭建 commentView样式分为两种     1.xib搭建界面   1.1 因为评论的样式大体上一样,我们可以用同一个xib来处理   1.2 最热评论   用 ...

  9. Winform 基本属性

    WinForm--- 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 代码部分在用户电脑上执行 WinFo ...

  10. int 与 Integer--话说数组转集合

    话说是自从JDK5后,而这就可以自动进行类型转换了. 当然,区别还是有的,就是对象和“非对象”什么的.可是,今天进行一个测试,出了一个小问题,现将代码贴下: 代码0:先来一个正常点的,用String进 ...