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. Ubuntu12.04+OpenERP6.1更改HTTP端口号为80

    在Ubuntu12.04中安装好OpenERP6.1以后,默认的端口号为8069,如果我们想改变为默认的80端口,可以通过如下方式处理. 1.首先通过iptables进行端口映射转换:sudo ipt ...

  2. 如何在 CentOS 7 中安装、配置和安全加固 FTP 服务

    步骤 1:安装 FTP 服务器 1. 安装 vsftpd 服务器很直接,只要在终端运行下面的命令. # yum install vsftpd 2. 安装完成后,服务先是被禁用的,因此我们需要手动启动, ...

  3. VC2012编译CEF3-转

    原文地址:http://blog.csdn.net/tiplip/article/details/42047815 下载 代码下载:http://cefbuilds.com/,CEF 3.2556.1 ...

  4. ocat 资源路径-时间控件

    http://www.htmleaf.com/jQuery/Calendar-Date-Time-picker/201504251737.html

  5. My Sql 高效分页

    /* *普通分页 *在数据文件上偏移1000000查出10条 */ select * from zoldesk_92game_net_ecms_bj where classid=303 ORDER B ...

  6. aop注解 spring提供的事务

    http://www.cnblogs.com/friends-wf/p/3826893.html 是 自定义的切面,并且添加注解 声明为切面 利用spring提供的事务声明 主要在 service层上 ...

  7. js ie下有效 showModalDialog 、showModelessDialog

    <input type="button" value="打开选择输入框"/> <script type="text/javascri ...

  8. xp 如何打开(进行)远程桌面连接

    http://apps.hi.baidu.com/share/detail/31102654http://help.360.cn/5030804/40072526.htmlhttp://hi.baid ...

  9. WordPress网站搬家的问题

    老邢的博客搬家全过程(wordpress搬家知识总结)   网站搬家过程中的几个问题   WordPress网站搬家的方法   WORDPRESS.ORG - zh-cn:WordPress 博客搬家 ...

  10. Eclipse折叠代码 coffee bytes code folding

    提供一个插件下载地址,博客园的: http://files.cnblogs.com/wucg/com.cb.eclipse.folding_1.0.6.jar.zip  将下载的zip文件解压出来的j ...