【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刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
随机推荐
- 查看apache,nginx,mysql,linux,php版本
查看apache版本 /usr/sbin/apachectl -v httpd -v 安装目录,使用apachectl -v mysql版本查看 mysql -V 查看linux版本 1.cat /e ...
- mac地址绑定
1.导入第三方类库: <?php /** * FILE_NAME : Macaddr.php * linux平台获取服务器mac地址 * @filesource */ class Macaddr ...
- python入门课程 第二章 安装Python
2-1 选择python版本首先python2.7和python3是不可以通用的目前丰富的类库都支持python2.7,所以选用Python2.7 选择python2.7版本2-2 window ...
- ASIHTTP
本文转载至 http://www.th7.cn/Program/IOS/201303/128223.shtml 向服务器端上传数据 ASIFormDataRequest ,模拟 Form表单提 ...
- 存储过程 & 触发器
触发器主要是通过事件进行触发而被执行的,而存储过程可以通过存储过程名字而被直接调用.当对某一表进行诸如UPDATE. INSERT. DELETE 这些操作时, 就会自动执行触发器所定义的SQL 语句 ...
- ABAP制作密码输入框
[转自 http://blog.csdn.net/saphome/article/details/6956911] 这几天做一个系统维护的程序,需要用户输入用户名和密码登录.可怎样实现输入密码显示星号 ...
- Datanode启动问题 FATAL org.apache.hadoop.hdfs.server.datanode.DataNode: Initialization failed for Block pool <registering>
-- ::, INFO org.apache.hadoop.hdfs.server.datanode.DataNode: supergroup = supergroup -- ::, INFO org ...
- 0522 HTML表单 CSS基础
一.列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: ul:unordered list,“无序列表”的意思. li:lis ...
- python安装包的方式
easy_install 老版python只有easy_install pip install 说明: 是easy_install的改进版,提供更好的信息提示,添加删除package等功能 安装方式: ...
- 20145229吴姗珊 《Java程序设计》第4周学习总结
20145229吴姗珊 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1继承共同行为 1.继承基本上就是避免多个类间重复定义共同行为 简单的类的定义 使用s ...