【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刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
随机推荐
- phalcon builder get raw sql
$this->modelsManager->createBuilder()->from('table')->where('a = "a"')->lim ...
- Codeforces Round #392 (Div. 2) F. Geometrical Progression
原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 se ...
- Collective Mindsets (medium) (逻辑题)
B - Collective Mindsets (medium) Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I6 ...
- java ScriptEngine 使用
Java SE 6最引人注目的新功能之一就是内嵌了脚本支持.在默认情况下,Java SE 6只支持JavaScript,但这并不以为着Java SE 6只能支持JavaScript.在Java SE ...
- BZOJ1791: [Ioi2008]Island 岛屿
BZOJ1791: [Ioi2008]Island 岛屿 Description 你将要游览一个有N个岛屿的公园. 从每一个岛i出发,只建造一座桥. 桥的长度以Li表示. 公园内总共有N座桥. 尽管每 ...
- centos6安装nginx最详细步骤
第一步:在centos下面下载 nginx wget http://nginx.org/download/nginx-1.2.9.tar.gz 解压 tar zxf nginx-1. ...
- json遍历
$.each(item.attributes,function (name,value) { });
- Android TextView文字过多时通过滚动条显示多余内容
方法一: TextView文字过多,显示不全,怎么办?我们可以为Textview添加滚动条. <TextView android:id="@+id/bus_detail_content ...
- [原创]java WEB学习笔记36:Java Bean 概述,及在JSP 中的使用,原理
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- Tab动画菜单
在线演示 本地下载