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.

思考:先首尾连成环,head前进(len-k%len)步,拆环。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *rotateRight(ListNode *head, int k) {if(head==NULL||k==0) return head;ListNode *p=head;int len=1;while(p->next){len++;p=p->next;}p->next=head;k%=len;int step=len-k;while(step--){p=p->next;}head=p->next;p->next=NULL;return head;}
};

  

转载于:https://www.cnblogs.com/Rosanna/p/3516145.html

[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 Array 旋转数组

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

  7. leetcode Rotate Image

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

  8. [LintCode/LeetCode] Rotate List

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

  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. python代码命令行tab补齐_Python在命令行下Tab键自动补全脚本
  2. Mybatis学习之配置优化
  3. Mimir:通过AI向所有人提供视频服务
  4. ABP vnext模块化架构的最佳实践的实现
  5. 【渝粤题库】广东开放大学 公共部门人力资源管理 形成性考核
  6. rem 之js代码获取font-size值(适合移动手机端)
  7. Jenkins环境搭建和部署项目的过程
  8. 【计组】超标量、超级流水线、超长指令字区别详解
  9. 基于Echarts+HTML5可视化数据大屏展示—智慧社区内网对比平台
  10. Win7系统电脑调节屏幕亮度的几种方法。
  11. tomcat 启动报错 registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister
  12. linux查看最后几行命令,linux查看文件的后几行(文件查看 如何显示最后几行 ,某几行)...
  13. Latex 制作表格出现以下错误 Extra alignment tab has been changed to \cr
  14. DICOM:基于DCMTK实现C-FIND SCU
  15. ubuntu16火狐下载文件提示“无法保存,因为无法读取源文件”
  16. QT编译libjpeg
  17. AM335X BeagleBone 之格式化SD卡
  18. 计算机毕业设计ssm图书出版系统0fes8系统+程序+源码+lw+远程部署
  19. ASP.Net ScriptManager 与 UpdatePanel
  20. 巴西柔术第五课:过腿

热门文章

  1. php 转换为自定义类,PHP面向对象教程之自定义类_PHP
  2. Android NDK开发之 NDK类型签名
  3. ECG的滤波和检波资源汇总
  4. JS事件流(事件冒泡 事件委托)
  5. 使用人脸客户端库快速实现对面部的分析---C#
  6. 【数据结构(C语言)】数据结构-树
  7. Hive练习之join
  8. Python包的相对导入时出现错误的解决方法
  9. linux文件描述符、软硬连接、输入输出重定向
  10. putty连接TPYBord V202开发板教程