【leetcode刷题笔记】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->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n 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的更多相关文章
- 【leetcode刷题笔记】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【leetcode刷题笔记】Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 【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 ...
- 【leetcode刷题笔记】Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【leetcode刷题笔记】Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【leetcode刷题笔记】Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
随机推荐
- Java引用类型作为形参和返回值
一.什么是引用类型 在Java中引用类型包括三种:类.抽象类.接口. 二.引用类型作为形参使用 1.类作为形参 /** * 类作为形参,实际传递的是该类的对象 */ class Student { p ...
- R语言数据分析系列之四
R语言数据分析系列之四 -- by comaple.zhang 说到统计分析我们就离不开随机变量,所谓随机变量就是数学家们为了更好的拟合现实世界的数据而建立的数学模型.有了她我们甚至能够来预測一个站点 ...
- USB设备驱动程序(一)
USB驱动编程原理: 当我们把USB设备插入USB口时会提示需要安装相对应的驱动,如USB鼠标.USB键盘等,这些电脑自己自身已经自带有相对于的驱动程序, 当电脑检查到该USB设备类型相同就去帮你安装 ...
- Fiddler 详尽教程与抓取移动端数据包
转载自:http://blog.csdn.net/qq_21445563/article/details/51017605 阅读目录 1. Fiddler 抓包简介 1). 字段说明 2). Stat ...
- 【BZOJ1061/3265】[Noi2008]志愿者招募/志愿者招募加强版 单纯形法
[BZOJ1061][Noi2008]志愿者招募 Description 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募 ...
- mysql时间相减的问题
MySQL中时间不能直接相减,如果日.分.时不同,相减结果是错误的 mysql> select t1,t2,t2-t1 from mytest; +--------------------- ...
- 2.设计模式---Adapter模式
Adapter模式也就是适配器模式,最常见的就是这个:码农必备-------------->笔记本电源适配器: 那么这玩意到底是干嘛的?? 手工画图一张: 220V--------------- ...
- 我的Java开发学习之旅------>Base64的编码思想以及Java实现
Base64是一种用64个字符来表示任意二进制数据的方法. 用记事本打开exe.jpg.pdf这些文件时,我们都会看到一大堆乱码,因为二进制文件包含很多无法显示和打印的字符,所以,如果要让记事本这样的 ...
- linux 基础2-null,cut,wc,head,tail
一. 特殊文件: /dev/null和/dev/tty Linux系统提供了两个对Shell编程非常有用的特殊文件,/dev/null和/dev/tty.其中/dev/null将会丢掉所有写入它的数据 ...
- 3.07课·········if分支语句
语句分类:顺序语句,选择语句(分支语句),循环语句 分支语句:(一)if(表达式) //表达式返回值是True或False{}说明:1.表达式返回的是bool值:2.小括号和花括号后面不需要加分号. ...