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 解题报告的更多相关文章

  1. LeetCode: Rotate Image 解题报告

    Rotate ImageYou are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ( ...

  2. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  3. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  4. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  5. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  6. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  7. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  8. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】396. Rotate Function 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rotate-fu ...

随机推荐

  1. SettingsEclipse&MyEclipse

      eclipse优化 迁移时间--2017年5月20日09:39:16 CreateTime--2016年11月18日11:27:02 Author:Marydon ModifyTime--2017 ...

  2. 【FAI】七日年化收益与万份收益的理解

    七日年化收益:其实指的一年的收益(应该忽略”七日”),这里的七日指的是取最近七日年化的结果 万份收益:每万元每天的收益 可以使用下图来清晰识别: 例子: 10000元按照5%的七日年化收益计算的话: ...

  3. iOS-高仿通讯录之商品索引排序搜索

    概述 TableView添加右侧索引, 将数据按照索引分组排序, 并添加搜索功能且在搜索界面复用当前页面. 详细 代码下载:http://www.demodashi.com/demo/10696.ht ...

  4. Google 做过的 12 件奇葩事

    Google做了太多伟大的事情了.以至于有时它有点让人难以实时跟上它的动态.假设你对这家公司略微有点感情.看看他们做过的一些有点匪夷所思的事儿,可能认为,毕竟是大公司.还挺难以被全然理解透的. 一个Q ...

  5. js insertBefore

    <select id="city" size="4" style="width:50px"> <option id=&qu ...

  6. js getAttribute getAttributeNode

    getAttribute():返回属性值,是一个文本字符串 getAttributeNode("属性名"):返回属性节点,是一个对象 <p id="bj" ...

  7. BIND9源码分析之acl 的实现

    BIND配置中一大堆一大堆的acl,什么allow-query, allow-recursion, allow-update还有view的match-clients等等等等. acl中的主要存储的就是 ...

  8. this.class.getClassLoader().getResourceAsStream与this.class.getResourceAsStream

    本文部分转自:http://xixinfei.iteye.com/blog/1256291 this.getClass().getClassLoader().getResource("tem ...

  9. matplot模块中的pylab

    pylab的目的 Pylab combines the functionality of pyplot with the capabilities of NumPy in a single names ...

  10. 《JAVA与模式》之适配器模式(转载)

    适配器模式比较简单,偷个懒,直接转载一篇. 个人理解: * 类适配器是通过继承来完成适配 * 对象适配器是通过传递对象来完成适配 * 不管哪种,其实都是通过引用特殊接口的对象来完成特殊接口的适配调用 ...