Problem

Given a list, rotate the list to the right by k places, where k is non-negative.

Example

Given 1->2->3->4->5 and k = 2, return 4->5->1->2->3.

Note

这是一道有故事的题目。
k有多大?其翼若垂天之云,则或长于链表。链表几长?北海也,天池也,其广数千里,则k亦可容于链表也。
故吾必先穷尽链表之长度,如若head为null,抑或其长可为k所整除,则此题不必再做,返回head可也。
而后吾当依除len取余之法,化大k为小k,则指针不致于越界也。后欲寻右起第k结点,令快指针先行数日,及至两指针相距为k,便吟鞭东指,与慢指针策马共进。则快指针行至null时,慢指针可至右起第k处矣。
方是时也,孤灯长夜,指飞键落。如风吹败叶,雨打窗棂。快慢指针亦止于其所焉。此时看官不妨温酒一盏,看那链表翻转,指针易位。那新头目唤作curhead,正是slow所指之人。fast舞动长剑,中宫直入,直取head首级,而slow一掌劈空,null已鸿飞冥冥。
自此,curhead一代天骄,霸业已成。

Solution

public class Solution {public ListNode rotateRight(ListNode head, int k) {ListNode fast = head, slow = head;int len = 0;while (fast != null) {fast = fast.next;len++;}if (head == null || k % len == 0) return head;fast = head;k = k % len;while (k-- > 0) fast = fast.next;while (fast.next != null) {fast = fast.next;slow = slow.next;}ListNode curhead = slow.next;slow.next = null;fast.next = head;return curhead;}
}

[LintCode/LeetCode] Rotate List相关推荐

  1. LeetCode——Rotate Image(二维数组顺时针旋转90度)

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

  2. LeetCode Rotate Array(数组的旋转)

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  3. LeetCode Rotate Image(矩阵的旋转)

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

  4. [LeetCode] Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  5. LeetCode:Rotate Image

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

  6. [LeetCode]Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  7. leetcode Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  8. leetcode Rotate Image

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

  9. LeetCode Rotate Function(寻找规律)

    题意:给出一个数组a,长度为n 其中f(0)=0*a[0]+1*a[1]+...+(n-1)*a[n-1] f(1)=0*a[n-1]+1*a[0]+...+(n-1)*a[n-2] ... f(n- ...

最新文章

  1. Google X的机器人开始打工!进咖啡馆擦桌子:擦的很慢,但我们一直在做
  2. JavaScript DOM编程艺术 - 读书笔记1-3章
  3. EasyExcel实现文件读取、导出、上传、下载操作
  4. ffmpeg摄像头推流
  5. 剑指Offer - 面试题60. n个骰子的点数(动态规划)
  6. 首届“马栏山杯”国际音视频算法优化大赛颁奖盛典暨高峰论坛圆满落幕
  7. linux web故障,网络故障处理与优化 linux服务器配置及故障排除 项目9 配置与管理web服务器.docx...
  8. 关于Scikit-Learn你(也许)不知道的10件事
  9. Linux Ansys
  10. 信息学奥赛一本通 铲雪车
  11. Python学习——三分钟分析目前最火的电视剧
  12. 【转】C++ 常用的STL查找函数方法
  13. Git Clone命令直接使用用户名密码Clone
  14. 记一些Linux/centos的基础运维命令
  15. 推荐一款优秀的硬盘空间管理工具软件-TreeSize Free
  16. html 一键复制 ios,h5实现一键复制到粘贴板-兼容ios
  17. C++学习笔记,坚持自律!
  18. Scrum立会报告+燃尽图(十月十日总第一次):选题
  19. MFC判断文件或文件夹是否存在函数
  20. 【iOS开发】---- 网站或技术论坛

热门文章

  1. linux系统乌班图18.04安装xenomai3.1,内核4.9.38
  2. 简谱打谱软件音乐梦想家与作曲大师有什么不同
  3. 大数据分析(一)——虚拟机环境配置
  4. SQL Server R2 报表服务,BIDS中预览带参数的报表,整个BIDS突然关闭
  5. asterisk 对接 移动IMS 外呼接通时返487错误(已解决)
  6. matlab制动,地铁机车牵引制动模型
  7. Moonbeam成功续约新一轮的Polkadot插槽租赁期
  8. 《三国演义》人物出场统计
  9. 【游戏精粹】独立非玩家角色合作行为的实现
  10. 网上花店销售系统(附源码+课件+讲解+资料+数据库)