LeetCode Rotate List
题意:给出一个单链表,将其右旋转k,其中k为非0
思路:首先统计单链表的结点数,因为k可能大于这个。将k转成小于单链表的结点数,先从头开始遍历k个,在继续遍历时,另一个从头开始,当遍历到尾时,另一个已经到离右边的第 k个。
代码如下:
class Solution
{public ListNode rotateRight(ListNode head, int k){if (null == head ) return head;int cnt = 0;ListNode cur = head, p = head;while (cur != null){cnt++;cur = cur.next;}k %= cnt;if (0 == k) return head;cnt = 0;cur = head;while (cnt++ < k){cur = cur.next;if (null == cur) return head;}while (cur.next != null){cur = cur.next;p = p.next;}ListNode ans = p.next;p.next = null;cur.next = head;return ans;}
}
LeetCode Rotate List相关推荐
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 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 ...
- LeetCode Rotate Image(矩阵的旋转)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- [LeetCode] Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- LeetCode:Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode]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 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- leetcode Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [LintCode/LeetCode] Rotate List
Problem Given a list, rotate the list to the right by k places, where k is non-negative. Example Giv ...
- 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- ...
最新文章
- oracle导入备份失败怎么办,ORACLE 数据备份、恢复以及导入时表空间不存在的解决方案...
- 玩转ios友盟远程推送,16年5月图文防坑版
- 会员管理scrm系统精细化运营更好促进成交
- 使用扩展的 CodeIgniter 框架实现 RESTful 框架
- html导航栏重叠怎么办,请问前端大神,html如何引入另一个html,写了一个导航栏想在多个页面中如何重复使用?...
- OPENCV3读取avi,解决返回NULL问题
- 深度学习必备:随机梯度下降(SGD)优化算法及可视化
- Ruby种的特殊变量
- 【直播提醒】荷小鱼:K12 在线教育应用的开发实践
- (3) ebj学习:有状态bean和无状态bean区别
- ps cs3中显示任何像素不大于50%选择。选区边将不可见是什么意思
- 电脑word在哪_到底哪个PDF转Word最好用?
- 网页控制台调用click()失败_C# 调用百度AI 人脸识别
- 第7课 阿布拉卡达布拉《小学生C++趣味编程》
- thoughtworks面试题分析与解答
- GO语言学习之路23
- APS傻瓜教材读后感之为什么需要人机交互调度
- VelocityTracker 滑动速度跟踪器 简介
- win10计算机分盘怎么设置密码,Win10如何限制磁盘分区被访问 Win10自带磁盘加密功能BitLocker在哪里...
- 关于穿越机FPV视频果冻效应的讨论
热门文章
- [css]我要用css画幅画(四)
- 序列联配(alignment)和数据库搜索方法简介
- 如何刪除GitHub中的repository
- 关于android Activity生命周期的说明
- python能用来做什么有意思的事情-用 Python 自动化办公能做到哪些有趣或有用的事情?...
- python字符串find函数-Python字符串的方法,查找和替换
- python代码壁纸-python实现壁纸批量下载代码实例
- python turtle库画图案-Python如何使用turtle库绘制图形
- python处理表格数据-Python数据处理(二):处理 Excel 数据
- 刚安装的python如何使用-python中RabbitMQ的使用(安装和简单教程)