【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
.
start with an example.
Given [0,1,2]
, rotate 1 steps to the right -> [2,0,1]
.
Given [0,1,2]
, rotate 2 steps to the right -> [1,2,0]
.
Given [0,1,2]
, rotate 3 steps to the right -> [0,1,2]
.
Given [0,1,2]
, rotate 4 steps to the right -> [2,0,1]
.
So, no matter how big K, the number of steps is, the result is always the same as rotating K % n
steps to the right.
public class Solution {
public ListNode rotateRight(ListNode head, int n) {
if(head==null||head.next==null||n==0)
return head;
int len = 0;
ListNode root = head;
while(root!=null){
root=root.next;
len++;
} ListNode fast = head;
ListNode slow = head;
int i=n%len;
if(i==0)
return head;
while(i>0&&fast!=null){
fast=fast.next;
i--;
} while(fast.next!=null){
slow=slow.next;
fast=fast.next;
} ListNode tem = slow.next;
fast.next=head;
slow.next=null;
return tem; }
}
【LeetCode】Rotate List的更多相关文章
- 【LeetCode】 Rotate List 循环链表
题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...
- 【leetcode】Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 【题解】【矩阵】【回溯】【Leetcode】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
随机推荐
- 制作不随浏览器滚动的DIV-带关闭按钮
制作不随浏览器滚动的DIV 效果见 http://bbs.csdn.net/topics/90292438 的滚动效果. $(function(){ //获取要定位元素距离浏览器顶部的距离 var ...
- 微信小程序 使用微信支付功能实现在线订单支付
以前做过PC页面微信支付,但是这次在小程序 直接调用微信支付功能还是方便很多 先放个微信官方API链接:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_a ...
- SPOJ GSS系列(数据结构维护技巧入门)
题目链接 GSS $GSS1$ 对于每个询问$l$, $r$,查询$a_{l}$, $a_{l+1}$, $a_{l+2}$, ..., $a_{r}$这个序列的最大字段和. 建立线段树,每个节点维护 ...
- ListView 在设备切换横竖屏时保存状态
比如listview在设备切换横竖屏时,仍然需要保证position, activity - > onSaveInstanceState - > restoreInstanceState ...
- xcopy中文文件名,中文件目录乱码问题
1.保存成bat脚本文件 2.且该bat文件不能使用utf-8格式,使用ANSI即正常
- Ubuntu 16.04安装Wine版的迅雷+QQ(完美方案,终极解决方法)
安装前先备份好系统! 继上一篇安装QQ的方法http://www.cnblogs.com/EasonJim/p/7425978.html,这一篇的QQ采用的是Wine模式安装.完美解决消息记录中文乱码 ...
- Linux 嵌入式启动以及优化
转载:http://www.embeddedlinux.org.cn/html/jishuzixun/201312/19-2717.html 第一步: BootLoader -- U boot 1 ...
- SpringUtils写法
@Componentpublic class SpringUtils implements ApplicationContextAware { @Override public void setApp ...
- C#报错"线程间操作无效: 从不是创建控件“XXX”的线程访问它"--解决示例
C# Winform程序中,使用线程对界面进行更新需要特殊处理,否则会出现异常“线程间操作无效: 从不是创建控件“taskView”的线程访问它.” 在网文“http://www.cnblogs.co ...
- angular - 编辑html文件-4
启动服务器: angular默认端口:4200 ng serve --port 3000 --open 输入本条命令后,会自动打开默认浏览器以及打开APP页 推荐开发工具webStorm,全平台兼容M ...