Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ m ≤ n ≤ length of list.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL

题意:

给定一个链表,反转第m~n个节点。

反转链表的一般思路

Solution1:

1.用指针找到m和n位置

2.反转m和n之间的链表

code

 class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head==null) return head; ListNode dummy = new ListNode(-1); dummy.next = head; ListNode mNode = head;
ListNode preM = dummy;
ListNode nNode = head; for (int i = 1; i < m ; i++) {
preM = mNode;
mNode = mNode.next;
} for (int i = 1; i <n ; i++) {
nNode = nNode.next;
} while(mNode != nNode){
preM.next = mNode.next;
mNode.next = nNode.next;
nNode.next = mNode;
mNode = preM.next;
}
return dummy.next;
}
}

[leetcode]92. Reverse Linked List II反转链表2的更多相关文章

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

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

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

  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倒置链表2 C++

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

  5. 92. Reverse Linked List II 反转链表 II

    网址:https://leetcode.com/problems/reverse-linked-list-ii/ 核心部分:通过a.b.c三个变量之间的相互更新,不断反转部分链表 然后将反转部分左右两 ...

  6. [Leetcode] Reverse linked list ii 反转链表

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...

  7. [LeetCode 92] Reverse Linked List II 翻转单链表II

    对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...

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

  9. LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)

    翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...

随机推荐

  1. IIS Express 域认证问题(https://stackoverflow.com/questions/4762538/iis-express-windows-authentication)

    option-1: edit \My Documents\IISExpress\config\applicationhost.config file and enable windowsAuthent ...

  2. int main(int argc,char* argv[]) 的含义和用法

    1.基本概念 argc,argv 用命令行编译程序时有用. 主函数main中变量(int argc,char *argv[ ])的含义,有些编译器允许将main()的返回类型声明为void,这已不再是 ...

  3. WebApp的自动测试工具: protractor和selenium

    Protractor是Selenium的扩充,支持Angularjs element(by.css('my-css')).click(); 一.用by的各种Locator定位元素 选中1个元素: el ...

  4. 使用python函数持续监控电脑cpu使用率、内存、c盘使用率等

    方法一: # import time 导入time模块 # import psutil 导入psutil模块 # def func(): # while True: ------->持续监控得w ...

  5. IntelliJ IDEA 打开项目红色

    项目目录红色主要是有版本控制所导致的,解决办法就是解除版本控制 点击File->Settings...->Version Control 项目会重新编译,项目目录红色会消失

  6. Linux安装rar

    1.下载RAR 官网下载地址:https://www.rarlab.com/download.htm wget下载:wget http://www.rarsoft.com/rar/rarlinux-x ...

  7. 代码:PC CSS(工作中用)

    常规内容区域的:标题和文字 2016-5-23 .p16{font-size:16px;color:#333;}/* 16号#333的标题 */ .p12-gray{font-size:16px;co ...

  8. LeetCode 101. Symmetric Tree 判断对称树 C++

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  9. jersey 开启gzip

    @Bean public ResourceConfig resourceConfig() { ResourceConfig resourceConfig = new ResourceConfig(); ...

  10. 在windows 10 上使用aspnet_regiis.exe -i 命令报 “此操作系统版本不支持此选项” 的解决办法

    用CMD窗口在C:\Windows\Microsoft.NET\Framework64\v4.0.30319下使用命令aspnet_regiis -i 报错: “此操作系统版本不支持此选项” .结果是 ...