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.


题解:

如上图所示,几个重要的变量:

prev记录m位置前一个位置上的ListNode,当完成2~5这一段的反转后,prev的next指针要设置成反转后的序列的第一个指针;

head记录要反转的子列表的第一个ListNode;

kepeler记录要反转的子列表的最后一个ListNode;

有一种特殊的情况如下图所示:

当要反转的序列的第一个ListNode就是链表的头结点的时候,prev指针为空。这时候反转后的链表头节点变成上述kepeler所指向的节点,所以要在最后单独处理。

代码中21~25行的循环找到prev和head的位置,27~31行的循环找到kepeler的位置,33~39行的for循环将链表制定范围的链表反转。41行进行判断:反转后序列的头节点是否变成要反转的列表的最后一个节点——kepeler,如果是,返回kepeler,否则返回之前保存的原链表头结点。

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
int HeadNodes = 0;
ListNode answer = head;
ListNode prev = null; if(head == null || m >= n)
return answer; while(HeadNodes+1 < m){
prev = head;
head = head.next;
HeadNodes++;
} ListNode kepeler = head;
while(HeadNodes+1 < n){
kepeler = kepeler.next;
HeadNodes++;
} for(int i = 0;i < n-m;i++){
ListNode temp = head.next;
head.next = kepeler.next;
kepeler.next = head; head = temp;
} if(prev == null)
return kepeler;
else{
prev.next = kepeler;
return answer;
}
}
}

【leetcode刷题笔记】Reverse Linked List II的更多相关文章

  1. 【leetcode刷题笔记】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  2. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  3. 【leetcode刷题笔记】Palindrome Partitioning II

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. 【leetcode刷题笔记】Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  5. 【leetcode刷题笔记】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  6. 【leetcode刷题笔记】Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. 【leetcode刷题笔记】Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. 【leetcode刷题笔记】Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. 【leetcode刷题笔记】Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  10. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

随机推荐

  1. 自己定义Application的未捕获异常处理

    近期由于工作原因.进行Android应用开发时发现应用在出现类似空指针等异常时,抛出未被捕获的异常.Android系统有默认的未捕获异常处理器,默认行为是结束对应的线程,但并不会直接退出程序,并且在应 ...

  2. python模块, 包的初识

    Python 模块(Module), 是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代 ...

  3. 浅谈<持续集成、持续交付、持续部署>(二)

    集成是指软件个人研发的部分向软件整体部分交付,以便尽早发现个人开发部分的问题:部署是代码尽快向可运行的开发/测试节交付,以便尽早测试:交付是指研发尽快向客户交付,以便尽早发现生产环境中存在的问题.如果 ...

  4. M - 基础DP

    M - 基础DP Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descriptio ...

  5. EasyPlayer RTSP播放器运行出现: Unable to load DLL 找不到指定的模块。exception from HRESULT 0x8007007E 解决方案

    最近有EasyPlayer RTSP播放器的开发者反馈,在一台新装的Windows Server 2008的操作系统上运行EasyPlayer RTSP播放器出现"Unable to loa ...

  6. SuperAgent使用文档

    SuperAgent是一个轻量级.灵活的.易读的.低学习曲线的客户端请求代理模块,使用在NodeJS环境中.官方文档:http://visionmedia.github.io/superagent 简 ...

  7. ibatis实现Iterate的使用 (转)

    <iterate         property="" /*可选,              从传入的参数集合中使用属性名去获取值,              这个必须是一 ...

  8. java 分布式锁 -图解- 秒懂

    目录 写在前面 1.1. 分布式锁 简介 1.1.1. 图解:公平锁和可重入锁 模型 1.1.2. 图解: zookeeper分布式锁的原理 1.1.3. 分布式锁的基本流程 1.1.4. 加锁的实现 ...

  9. Android获取系统外置存储卡路径的方法

    android系统可通过Environment.getExternalStorageDirectory()获取存储卡的路径.可是如今有非常多手机内置有一个存储空间.同一时候还支持外置sd卡插入,这样通 ...

  10. PHP环境变量归纳(转自网络)

    PHP环境变量主要有$GLOBALS[].$_SERVER[].$_GET[].$_POST[].$_COOKIE[].$_FILES[].$_ENV[].$_REQUEST[].$_SESSION[ ...