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.

 class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null) return head;
ListNode fakehead = new ListNode(0);
fakehead.next = head;
ListNode pre = fakehead; for (int i = 0; i < m-1;i++) pre = pre.next; ListNode start = pre.next;
ListNode then = start.next; for(int i = 0 ;i < n -m ;i++){
start.next= then.next;
then.next = pre.next;
pre.next = then;
then=start.next;
}
return fakehead.next;
}
}

92. Reverse Linked List II(链表部分反转)的更多相关文章

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

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

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

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

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

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

  7. [leetcode]92. Reverse Linked List II反转链表2

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  8. 92. Reverse Linked List II 翻转链表II

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  9. [LeetCode]92. Reverse Linked List II反转部分链表

    /* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...

随机推荐

  1. POJ 1742 Coins(多重背包, 单调队列)

    Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...

  2. 漫谈.Net关键字系列之一Sealed与Final(转)

    转自:http://www.cnblogs.com/isline/archive/2010/08/31/1813396.html Sealed与Final修饰符其实并不是一个语言平台的产物,他们有着各 ...

  3. Python IDLE背景设置与使用

    相信刚进入python学习之路的朋友们,都还是挺喜欢python自带的IDLE,但是白的代码背景色以及其它的代码色确实让人看着有点不舒服,所以当时也琢磨着能不能自己给它换换颜色,这个当然可以,废话不多 ...

  4. 浅谈 SSD,eMMC,UFS(转自知乎)

    但作为一个计算机体系结构的研究生,在这些名词满天飞的时候,我的好奇心是抑制不住的,想一探这几样技术的究竟.本文不对某一特定事件进行点评,仅从技术角度分析对比一下这三种技术.就算是当做自己的技术储备+科 ...

  5. Android 使用ProgressBar实现进度条

    ProgressBar简介ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好型. 课程目标(1)制定Progre ...

  6. openstack将本地实例迁移至ceph存储中

    需求: 最近在openstack上线了ceph存储,创建虚拟机和云硬盘都基于ceph卷进行存储和创建,但是之前openstack用的是本地存储,创建的所有实例都在本地文件中,当实例重启之后,opens ...

  7. LeetCode——Best Time to Buy and Sell Stock III

    Description: Say you have an array for which the ith element is the price of a given stock on day i. ...

  8. Mysql group_concat函数列转行,与行转列

    例一: SELECT num from user 1.使用group_concat函数得到列转行 select group_concat(num) from user 2.使用SUBSTRING_IN ...

  9. NIO之Buffer的clear()、rewind()、flip()方法的区别

    Java的NIO中有关Buffer的几种常用方法比如clear,rewind和flip到底有哪些区别.下面给大家这三种方法的源码,方便大家记忆.clear()方法用于写模式,其作用为情况Buffer中 ...

  10. redis数据持久化(快照/日志):

    1.RDB快照的配置选项: save // 900内,有1条写入,则产生快照 save // 如果300秒内有1000次写入,则产生快照 save // 如果60秒内有10000次写入,则产生快照 ( ...