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. 架构师技能图谱 V1.2

    系统架构能力 基本理论 扩展性设计 可用性设计 可靠性设计 一致性设计 负载均衡设计 过载保护设计 灾难恢复和备份 协议设计 二进制协议 文本协议 接入层架构设计 DNS 轮询 动静态分离 静态化 反 ...

  2. Linux下安装docker(1)

    1.由于centos系统已经自带docker源了,所以可以直接安装: yum install docker 如果是centos6.5版本的,使用yum -y install docker-io 进行安 ...

  3. Android 开发 上拉加载更多功能实现

    实现思维 开始之前先废话几句,Android系统没有提供上拉加载的控件,只提供了下拉刷新的SwipeRefreshLayout控件.这个控件我们就不废话,无法实现上拉刷新的功能.现在我们说说上拉加载更 ...

  4. thingsboard填坑之路

    因为thingsboard都是国外的资料,国内基本没有参考资料.所以,记录下来源码安装当中,遇到的问题. thingsboard官网源码安装连接: https://thingsboard.io/doc ...

  5. ASP.NET 实现PDF文件下载[转]

    本文介绍了一种在ASP.NET中下载文件的方法.方法一:可能是最简单的.最短的方式: 1 Response.ContentType = "application/pdf"; 2 R ...

  6. dubbo . dubbo Please check registry access list (whitelist/blacklist) 错误

    dubbo Please check registry access list (whitelist/blacklist) dubbo服务调用provider失败.解决办法: 再consumer的服务 ...

  7. [Android]Linux下WebRTC下载与编译

    1.硬盘空间: WebRTC官方原话: The checkout size is large due the use of the Chromium build toolchain and many ...

  8. swift 加载 本地html 和 网络路径

    先上代码: xcode 9.4  ios 11.4 import UIKit import WebKit class RootViewController: UIViewController, WKN ...

  9. 如何系统的学习Java

    初学者记住一点,学习Java一定是连续性的且循序渐进的“系统化”学习,首先我给你提供一个优秀Java工程师的学习路线. web前端方面:html.css,Java.jQuery.xml解析.Boots ...

  10. Matplotlib-多图合并显示

    Subplot 多合一显示 均匀图中图 不均匀图中图 # 均匀图中图 # matplotlib 是可以组合许多的小图, 放在一张大图里面显示的. 使用到的方法叫作 subplot. # 使用impor ...