这题我的第一想法是用头插法,但实际上并不好做,因为每次都需要遍历最后一个。更简单的做法是将其连成环,找到相应的位置重新设头结点和尾结点。这过

有很多细节需要注意,比如K有可能是大于链表长度的,如何重新设置K等都要注意。

 ListNode *rotateList(ListNode *head, int k){if (head == nullptr || k == 0)return head;ListNode *p = head;int n = 1;while (p->next){n++;p = p->next;}k =n - k%n;//k有可能大于np->next = head;for (int i = 0; i < k; i++)p = p->next;head = p->next;p->next = nullptr;return head;}

View Code

转载于:https://www.cnblogs.com/573177885qq/p/5508473.html

leetcode 之Rotate List(18)相关推荐

  1. [勇者闯LeetCode] 189. Rotate Array

    [勇者闯LeetCode] 189. Rotate Array Description Rotate an array of n elements to the right by k steps. F ...

  2. leetcode 61 Rotate List ----- java

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

  3. [LeetCode 题解]: Rotate List

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

  4. leetcode 796. Rotate String | 796. 旋转字符串(KMP)

    题目 https://leetcode.com/problems/rotate-string/ 题解 左神讲的 KMP class Solution {public boolean rotateStr ...

  5. leetcode 396. Rotate Function | 396. 旋转函数(错位相减)

    题目 https://leetcode.com/problems/rotate-function/ 题解 参考了评论区 O(n)错位相减 的题解. class Solution {public int ...

  6. Java for LeetCode 061 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

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

  8. LeetCode 61. Rotate List

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

  9. LeetCode 189. Rotate Array

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

最新文章

  1. oracle视图能用etl工具_(转)使用kettle作为sqlserver2008和oracle10G之间的ETL工具
  2. linux 重新分区挂载,Linux:挂载磁盘分区,linux已挂载磁盘重新分区
  3. BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
  4. POJ 1655 Balancing Act (求树的重心)【树形DP】(经典)
  5. 使用WebApiClient请求和管理Restful Api
  6. Centos7 Docker私有仓库搭建
  7. MySQL_存储引擎
  8. html%3c转译,[转]URL编码
  9. mplayer命令行模式下的使用方法
  10. Logisim元件用法详解五:Memory 储存库
  11. 改变翻天覆地 史上最全Fermi架构解读
  12. 分享一个超酷创建互动文档的Javascript类库 - tangle
  13. 让你的工作变轻松的一套免费的 iPhone 手势图标
  14. 面试中被问到“为什么从上一个单位离职”怎么回答
  15. 你在日本经历过哪些事情的体验不如中国?
  16. 掌握五个元组的用法,让python代码飞得更快
  17. 【Gradle】Gradle运行main方法报错:Unnecessarily replacing a task that does not exist is not supported.
  18. 账户结构,推广计划,推广单元
  19. Remix-IDE安装开发环境与使用文档(Windows环境)
  20. syntax error, unexpected ‘array‘ (T_ARRAY)

热门文章

  1. Lesson6 how to use HTML QT Widgets
  2. 神经网络填充(padding)
  3. 元组可以直接添加进数据库吗_AWS Neptune 详细体验:OLTP的可扩展图数据库
  4. mysql5.7.20 sql mode_MySQL5.7中的sql_mode默认值带来的坑及解决方法
  5. html验证用户名长度,JQuery表单字符串长度验证
  6. 每天执行一次批处理_关于静态批处理/动态批处理/GPU Instancing /SRP Batcher的详细剖析...
  7. python 视频分析_成为视频分析专家:自动生成集锦的方法(Python实现)
  8. 微信hash ajax,基于vue hash模式微信分享#号的解决
  9. glShadeModel
  10. centos java7安装_Centos7 JDK7安装配置