leetcode 之Rotate List(18)
这题我的第一想法是用头插法,但实际上并不好做,因为每次都需要遍历最后一个。更简单的做法是将其连成环,找到相应的位置重新设头结点和尾结点。这过
有很多细节需要注意,比如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)相关推荐
- [勇者闯LeetCode] 189. Rotate Array
[勇者闯LeetCode] 189. Rotate Array Description Rotate an array of n elements to the right by k steps. F ...
- 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 ...
- [LeetCode 题解]: Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- leetcode 796. Rotate String | 796. 旋转字符串(KMP)
题目 https://leetcode.com/problems/rotate-string/ 题解 左神讲的 KMP class Solution {public boolean rotateStr ...
- leetcode 396. Rotate Function | 396. 旋转函数(错位相减)
题目 https://leetcode.com/problems/rotate-function/ 题解 参考了评论区 O(n)错位相减 的题解. class Solution {public int ...
- 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 ...
- 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 ...
- LeetCode 61. Rotate List
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- 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 ...
最新文章
- oracle视图能用etl工具_(转)使用kettle作为sqlserver2008和oracle10G之间的ETL工具
- linux 重新分区挂载,Linux:挂载磁盘分区,linux已挂载磁盘重新分区
- BeautifulSoup的高级应用 之 contents children descendants string strings stripped_strings
- POJ 1655 Balancing Act (求树的重心)【树形DP】(经典)
- 使用WebApiClient请求和管理Restful Api
- Centos7 Docker私有仓库搭建
- MySQL_存储引擎
- html%3c转译,[转]URL编码
- mplayer命令行模式下的使用方法
- Logisim元件用法详解五:Memory 储存库
- 改变翻天覆地 史上最全Fermi架构解读
- 分享一个超酷创建互动文档的Javascript类库 - tangle
- 让你的工作变轻松的一套免费的 iPhone 手势图标
- 面试中被问到“为什么从上一个单位离职”怎么回答
- 你在日本经历过哪些事情的体验不如中国?
- 掌握五个元组的用法,让python代码飞得更快
- 【Gradle】Gradle运行main方法报错:Unnecessarily replacing a task that does not exist is not supported.
- 账户结构,推广计划,推广单元
- Remix-IDE安装开发环境与使用文档(Windows环境)
- syntax error, unexpected ‘array‘ (T_ARRAY)
热门文章
- Lesson6 how to use HTML QT Widgets
- 神经网络填充(padding)
- 元组可以直接添加进数据库吗_AWS Neptune 详细体验:OLTP的可扩展图数据库
- mysql5.7.20 sql mode_MySQL5.7中的sql_mode默认值带来的坑及解决方法
- html验证用户名长度,JQuery表单字符串长度验证
- 每天执行一次批处理_关于静态批处理/动态批处理/GPU Instancing /SRP Batcher的详细剖析...
- python 视频分析_成为视频分析专家:自动生成集锦的方法(Python实现)
- 微信hash ajax,基于vue hash模式微信分享#号的解决
- glShadeModel
- centos java7安装_Centos7 JDK7安装配置