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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ 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) {
int d=n-m;
if(head==NULL||head->next==NULL||d<=)
return head; ListNode* p=head,*bfp=p;
ListNode* q=head;
for(int i=;i<m-;i++){
bfp=p;
p=p->next;
}
ListNode* revhead=p;
ListNode* bf=p,*bh=NULL;
p=p->next;
d--;
while(p->next!=NULL&&d!=){
d--;
bh=p->next;
p->next=bf;
bf=p;
p=bh; }
bh=p->next;
p->next=bf;
bfp->next=p;
revhead->next=bh;
if(m!=)
return head;
else return p;
}
};

【leetcode】92. Reverse Linked List II的更多相关文章

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

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

  2. 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List

    The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ...

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

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

  4. 【LeetCode】206. Reverse Linked List (2 solutions)

    Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...

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

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

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

  7. 【LeetCode】206. Reverse Linked List

    题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...

  8. 【Lintcode】036.Reverse Linked List II

    题目: Reverse a linked list from position m to n. Given m, n satisfy the following condition: 1 ≤ m ≤ ...

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

随机推荐

  1. Ajax 向后台提交一个 JavaScript 对象数组?

    var postArray= new Array(); var temp = new Object(); temp.id='1'; temp.name='test'; postArray.push(t ...

  2. Vijos 1111 小胖的水果 LCS

    描述 xuzhenyi到大同水果店去买水果,但老板huyichen告诉他每次只能买一种,但是xuzhenyi想吃两种,于是在讨价还价之后,huyichen说只要xuzhenyi能把他想要的两种水果合并 ...

  3. jpg、jpeg、png... 的区别

    对于做设计这一行的人来说,这几个图片格式是最常用的,也是最常见的,几乎每一天都要与他们打交道. 刚刚入门的新人通常不知道在什么地方如何使用他们或者说如何更有效的使用他们. 那他们到底是有什么区别?(一 ...

  4. git分支的使用

    本文章假定你已经接触了一些git的基本概念和基本的操作知识 这里先贴出关于分支的一些常用命令 git branch /*查看所有分支*/git branch <branch-name> / ...

  5. python多线程爬虫设计及实现示例

    爬虫的基本步骤分为:获取,解析,存储.假设这里获取和存储为io密集型(访问网络和数据存储),解析为cpu密集型.那么在设计多线程爬虫时主要有两种方案:第一种方案是一个线程完成三个步骤,然后运行多个线程 ...

  6. window.onload 和 $(document).ready()

    一. window.onload 1. 必须等到页面上所有元素(包括图片, JS文件,CSS文件等外部资源)加载完成后才执行 2. window.onload绑定多个函数时,只会执行最后一个 < ...

  7. Java用Cookie简单限制点赞次数

    楼主最近在搞一个当下比较流行的点赞功能,这个功能也是让程序员又爱又恨啊 说起爱,点赞是个社会化的动作,全民都在为美好的事情,行为,动作,点赞. 说起恨,你很难在用户没有登录的情况下限制恶意点赞的机器人 ...

  8. linux DNS 问题

    今天准备爬虫51job时候,发现ping不通外网了,ping 了一下IP,都是OK的,只是host不通. 呵呵,就一DNS问题,好的.第一步,开始检查配置文件 cat /etc/sysconfig/n ...

  9. nopCommerce 3.9 大波浪系列 之 NUnit 配置调试环境

    官网:http://nunit.org/ GitHub:https://github.com/nunit 本文只介绍NUnit在VS中的配置使用,进一步学习 NUnit 请参考官方文档. nopCom ...

  10. 51nod_1639:绑鞋带

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1639 #include <bits/stdc++.h& ...