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