题目:

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.  1    public ListNode rotateRight(ListNode head, int n) {
  2.  2         if(head==null||head.next==null||n==0)
  3.  3             return head;
  4.  4         ListNode fast = head, slow = head,countlen = head;
  5.  5         ListNode newhead = new ListNode(-1);
  6.  6         int len = 0;
  7.  7         
  8.  8         while(countlen!=null){
  9.  9             len++;
  10.              countlen = countlen.next;
  11.          }
  12.          
  13.          n = n%len;
  14.          if(n==0)
  15.              return head;
  16.          
  17.          for(int i = 0; i < n; i++)
  18.              fast = fast.next;
  19.          
  20.          while(fast.next!=null){
  21.              slow = slow.next;
  22.              fast = fast.next;
  23.          }
  24.          
  25.          newhead = slow.next;
  26.          fast.next = head;
  27.          slow.next = null;
  28.          
  29.          return newhead;
  30.      }

解法2:

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

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. OpenCV 基础笔记

    本文大部分内容来源于入门者的Python快速教程 - 番外篇之Python-OpenCV 本篇将介绍和深度学习数据处理阶段最相关的基础使用,并完成4个有趣实用的小例子: 延时摄影小程序 视频中截屏采样 ...

  2. kolla部署all-in-one环境(N版)

    简单介绍: Kolla 是 OpenStack 大帐篷模式下的官方子项目之一,其主要目标是通过利用 Docker 容器以及 Ansible 自动化部署工具,来为 OpenStack 云平台提 供一个简 ...

  3. python opencv3 滤波器 卷积核

    git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 import numpy as np from sc ...

  4. python opencv3 显示一张图片

    git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 显示一张图像 ...

  5. [Arc058E] Iroha and Haiku

    [Arc058E] Iroha and Haiku 题目大意 问有多少\(n\)个数的正整数序列,每个数在\([1,10]\)之间,满足存在\(x,y,z,w\)使得\(x\to y-1,y\to z ...

  6. 【POJ】2796:Feel Good【单调栈】

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18449   Accepted: 5125 Case T ...

  7. Codeforces Round #196 (Div. 2) A. Puzzles 水题

    A. Puzzles Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showProblem ...

  8. HDU 4268 Alice and Bob 贪心STL O(nlogn)

    B - Alice and Bob Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  9. Codeforces Round #293 (Div. 2) A. Vitaly and Strings

    A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...

  10. 记一次完整的pc前端整站开发

    我所做的项目是一个线上的旅游平台,当然不是大家耳熟能详的那些旅游平台了,项目刚开始也没有必要去评价它的好坏,在这里我就不多说了~这是线上地址有兴趣的同学可以去看看(www.bookingctrip.c ...