题目:

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.

题解:

这道题主要先理解题意,就是倒着数k个node,从那开始到结尾和之前那部分对调,那个例子就是,4->5拿前面来,1->2->3拿后面去。

几个特殊:

k是可以大于整个list的长度的,所以这时要对k对len取模

如果取模之后得0,相当于不用rotate,直接返回

处理完特殊情况后,就用熟悉的faster/slower双指针解决就好(看到这种linkedlist,倒着数数的,就条件反射了)

先对faster设步长为n,然后faster和slower再一起走,知道faster.next==null,说明slower指向要倒着数的开始点的前一个位置。

所以slow.next就是要返回的newhead,保存一下。

然后把faster.next接到head上,slower.next存为null,作队尾。

这样就把list给rotate了。

这是我想的一种解法,还有一种就是把整个list连起来,变成环,找到切分点断开就行。

解法1:

 1    public ListNode rotateRight(ListNode head, int n) {
 2         if(head==null||head.next==null||n==0)
 3             return head;
 4         ListNode fast = head, slow = head,countlen = head;
 5         ListNode newhead = new ListNode(-1);
 6         int len = 0;
 7         
 8         while(countlen!=null){
 9             len++;
             countlen = countlen.next;
         }
         
         n = n%len;
         if(n==0)
             return head;
         
         for(int i = 0; i < n; i++)
             fast = fast.next;
         
         while(fast.next!=null){
             slow = slow.next;
             fast = fast.next;
         }
         
         newhead = slow.next;
         fast.next = head;
         slow.next = null;
         
         return newhead;
     }

解法2:

 1 public ListNode rotateRight(ListNode head, int n) {
 2 
 3     if (head == null || n == 0)
 4         return head;
 5     ListNode p = head;
 6     int len = 1;//since p is already point to head
 7     while (p.next != null) {
 8         len++;
 9         p = p.next;
     }
     p.next = head; //form a loop
     n = n % len;
     for (int i = 0; i < len - n; i++) { 
         p = p.next;
     } //now p points to the prev of the new head
     head = p.next;
     p.next = null;
     return head;
 }

Reference for 2: http://leetcodenotes.wordpress.com/2013/08/14/leetcode-rotate-list-%E6%8A%8A%E5%90%8Ek%E4%B8%AArotate%E5%88%B0list%E5%89%8D%E9%9D%A2%E5%8E%BB%EF%BC%8Ck%E5%8F%AF%E4%BB%A5%E8%B6%85%E8%BF%87list%E6%9C%AC%E8%BA%AB%E9%95%BF%E5%BA%A6/

Rotate List leetcode java的更多相关文章

  1. Rotate Image leetcode java

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  2. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  3. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  4. LeetCode算法题-Rotate Array(Java实现)

    这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...

  5. Regular Expression Matching leetcode java

    题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...

  6. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  7. 189. Rotate Array - LeetCode

    Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...

  8. 796. Rotate String - LeetCode

    Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...

  9. 48. Rotate Image - LeetCode

    Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...

随机推荐

  1. Django项目从零开始的大概脉络

    Django项目从零开始脉络 创建虚拟环境,隔离项目python环境:mkvirtualenv -p /usr/bin/python3.6 envname 安装Django:pip install d ...

  2. Jenkins的Pipeline脚本在美团餐饮SaaS中的实践

    一.背景 在日常开发中,我们经常会有发布需求,而且还会遇到各种环境,比如:线上环境(Online),模拟环境(Staging),开发环境(Dev)等.最简单的就是手动构建.上传服务器,但这种方式太过于 ...

  3. libhiredis.so.0.13: cannot open shared object file: No such file or director

    Hiredis安装步骤: tar zxvf antirez-hiredis-v0.10.1-0-g3cc6a7f.zip cd antirez-hiredis-3cc6a7f make 解决办法: m ...

  4. 1012 The Best Rank (25)(25 point(s))

    problem To evaluate the performance of our first year CS majored students, we consider their grades ...

  5. windows下thrift的使用(C++)

    thrift cpp环境搭建: 1.  安装boost_1_53_0,注意,使用vs2010版本时,使用二进制的boost安装版本,生成的lib有可能是,在后续操作会出问题.在源码目录中,运行boot ...

  6. Collection模块

    一.nametuple--factory function for creating tuple subclasses with named fields 创建类似于元祖的数据类型,除了能够用索引来访 ...

  7. Xshell6和Xftp下载地址,rzsz的使用

    官方下载地址:https://cdn.netsarang.net/98f59c09/Xshell-6.0.0076r_beta.exe https://cdn.netsarang.net/98f59c ...

  8. 【贪心】Codeforces Round #480 (Div. 2) C. Posterized

    题意:让你对[0,255]这个序列任意划分成一些不重叠的子段,每个子段的大小不超过K.给你n个不超过255的数,让你将每个数替换成它所在子段的任意一个元素,使得最终这个n个数的序列的字典序最小. p[ ...

  9. shell十三问?

    shell 十三问: 1) 为何叫做 shell ?  2) shell prompt(PS1) 与 Carriage Return(CR) 的关系?  3) 别人 echo.你也 echo ,是问 ...

  10. ListView实现下拉刷新功能

    很久没有写博客了,感觉都懒惰了,今天说一下一个自定义的空间,就是ListView下拉列表可以刷新的功能,相信很多同学都看到过这种功能,最典型的就是新浪微博的下拉刷新列表了. 废话不多说,首先呢,下拉刷 ...