题目:

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. [BZOJ4700]适者(CDQ分治+DP/李超线段树)

    如果没有秒杀,就是经典的国王游戏问题,按t/a从小到大排序即可. 考虑删除两个数i<j能给答案减少的贡献:S[i]*T[i]+P[i-1]*A[i]-A[i]+S[j]*T[j]+P[j-1]* ...

  2. BZOJ2120数颜色(带修改莫队)

    莫队算法是一种数据结构的根号复杂度替代品,主要应用在询问[l,r]到询问[l+1,r]和[l,r+1]这两个插入和删除操作复杂度都较低的情况下.具体思想是:如果把一个询问[l,r]看做平面上的点(l, ...

  3. hdu 4549 矩阵快速幂

    题意: M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F ...

  4. Ajax 的概念及过程?Ajax 的交互模型?同步和异步的区别?如何解决跨域问题?

    Ajax 是什么: 1)  通过异步模式,提升了用户体验 2)  优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用 3)  Ajax 在客户端运行,承担了一部分本来由服务器承担的工 ...

  5. Elasticsearch 横向扩容以及容错机制

    写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...

  6. IEEE Bigger系列题解

    Bigger系列题解 Bigger Python 坑点在于要高精度以及表达式求值,用java写可以很容易避免高精度问题 然后这道题就可以AC了 代码 import java.io.*; import ...

  7. Codeforces Round #279 (Div. 2) C. Hacking Cypher 机智的前缀和处理

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...

  8. 使用yum高速部署Oracle安装环境(11g)

    基于Linux安装过Oracle的童鞋们都应该清楚,安装Oracle的确是一件比較费时费力的差事,由于不过前期的rpm包,内核參数,创建用户等等这些个步骤都让那些新手不免眼花缭乱,一不留神.就导致终于 ...

  9. Send a WhatsApp Message programatically -- Tasker WhatsTasker

    Here is My code snippet: Uri mUri = Uri.parse("smsto:+9876543210"); Intent mIntent = new I ...

  10. MAX II Device Compatibility with 5.0-V CMOS Devices

    http://www.altera.com/literature/hb/max2/max2_mii51009.pdf The open-drain pin never drives high, onl ...