LeetCode: Rotate List 解题报告
Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
SOLUTION 1:
重点就是,要先变成循环链表,end.next = head;再执行ListNode headNew = pre.next; 否则,当n = 0的时候,会返回一个null指针,因为pre是在最后的。
Rotate的精髓是旋转,也就是说当n=0的时候,应该什么也不做,那么pre的下一个应该是头节点。所以我们应该把end.next = head;
另外的做法,就是把n = 0单独拿出来,当n = 0 直接returen head. 这样子就不用考虑这种特殊情况了。pre.next就一定不会是null.
public ListNode rotateRight1(ListNode head, int n) {
if (head == null) {
return head;
}
int len = getLen(head);
// 不需要重复地rotate.
n = n % len;
ListNode end = head;
while (n > 0) {
end = end.next;
n--;
}
ListNode pre = head;
while (end.next != null) {
pre = pre.next;
end = end.next;
}
// 这一句很重要,变成循环链表后,可以处理n = 0的情况。因为尾节点的下一个节点是头节点
end.next = head;
ListNode headNew = pre.next;
pre.next = null;
return headNew;
}
public int getLen(ListNode head) {
int len = 0;
while (head != null) {
len++;
head = head.next;
}
return len;
}
LeetCode: Rotate List 解题报告的更多相关文章
- LeetCode: Rotate Image 解题报告
Rotate ImageYou are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】396. Rotate Function 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...
随机推荐
- 〖Linux〗Android NDK调用已编译好的C/C++动态连接库(so文件)
一.背景:假定已有应用程序zigbeeclient.cpp,内容如下: ... extern "C" { int getresult(int argc, char **argv); ...
- CUDA 7.0 速查手册
Create by Jane/Santaizi 03:57:00 3/14/2016 All right reserved. 速查手册基于 CUDA 7.0 toolkit documentation ...
- POJ 3468 A Simple Problem with Integers 【树状数组】
题目链接:id=3468">http://poj.org/problem?id=3468 题目大意:给出一组数组v[i],有两种操作,一种给出两个数a,b.要求输出v[a]到v[b]之 ...
- 原生php如何获取当前页面的url
原生php如何获取当前页面的url? //php获取当前访问的完整url地址 function get_current_url(){ $current_url='http://'; if(isset( ...
- PHP-PHP核心技术与最佳实践阅读
1.对象的实质: 对象就是数据, 对象本身不包含方法, 但是对象有一个"指针"指向一个类, 这个类里可以有方法 2.反射是指在PHP运行状态中, 扩展分析PHP程序, 导出或者提取 ...
- 【C++】关于带const的指针问题
区分const出现在*前还是*后 前: 例如const int *p,这种表示情况下,p本身可以改变,即p可以指向不同的地址, 但是p指向的内容不可改变. 就像你喜欢看书,图书馆规定你可以任意借阅及更 ...
- Linux命令-查看进程命令:pstree
查看进程树,ps aux查看进程,如果进程太多看起来很不方便,可以使用pstree以树形方式显示正在运行的所有进程 pstree -p 查看进程树 还是太多了,可以使用管道符进行查找httpd(apa ...
- OAF_OAF增删改-删除的实现(案例)
2014-06-02 Created By BaoXinjian
- 谈谈CListCtrl如何调整行高
原文链接: http://blog.csdn.net/sstower/article/details/9094939 调整CListCtrl 行高通常有3种方法: 1.设定字体2.设定图片3.处理Me ...
- webpack-dev-server的自动更新配置
一.背景 测试发布一个项目,修改jsx中的内容,页面不自动更新. 每次必须执行npm run build:然后执行npm run start. 脚本如下: "scripts": { ...