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的更多相关文章

  1. 【LeetCode】 Rotate List 循环链表

    题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...

  2. 【leetcode】Rotate Image

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  3. 【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 ...

  4. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  6. 【题解】【矩阵】【回溯】【Leetcode】Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  7. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  8. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  9. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

随机推荐

  1. ASP.NET路由应用及IIS配置(非MVC)

    一.前后台代码: Global.cs: using System.Web.Routing; ... void Application_Start(object sender, EventArgs e) ...

  2. FZU 2122 又见LKity【字符串/正难则反/KMP/把一个字符串中某个部分替换为另一个部分】

    嗨!大家好,在TempleRun中大家都认识我了吧.我是又笨又穷的猫猫LKity.很高兴这次又与各位FZU的ACMer见面了.最近见到FZU的各位ACMer都在刻苦地集训,整天在日光浴中闲得发慌的我压 ...

  3. lms111,rplidar 方向和起始角

    上图中,从X反方向是开始,按顺时针方向增加,实际运转方向也为顺时针方向. lms111:正放时:数据按逆时针依次输出.(起始----->结束) 北阳:正放时:数据按逆时针依次输出

  4. ConcurrentHashMap如何保证线程安全

    以前看过HashMap的内部实现,知道HashMap是使用Node数组+链表+红黑树的数据结构来实现,如下图所示.但是HashMap是非线程安全,在多线程环境不能够使用. 不过JDK在其并发包中为我们 ...

  5. sublime正则替换

    [^a-zA-Z',=]+ 若文本中只有字母和汉字,则上式可用来匹配非字母的中文

  6. sshpass结合ssh和scp可以自动完成密码登录,无需手动输入密码

    使用方法: 1.sshpass -p 123456 ssh admin@1.1.1.1 "touch file"  远程创建文件file 2.sshpass -p 123456 s ...

  7. win7 更改同步时间的网址

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\D ...

  8. jquery中美元符号($)命名冲突

    http://blog.csdn.net/shanshan209/article/details/6341727 在Jquery中,$是JQuery的别名,所有使用$的地方也都可以使用JQuery来替 ...

  9. Net调用非托管代码(P/Invoke与C++InterOP) [转]

    将 System::String 转换为 wchar_t* 或 char* PtrToStringChars将String转换为本机wchar_t *或char *.由于 CLR 字符串为内部 Uni ...

  10. SolidEdge 打开工程图提示图纸已过期怎么办

    如下图所示,打开工程图时提示图纸已过期   点击工具-图纸视图跟踪器,按提示打开过期的装配体文件   更新这个装配体文件   然后切换到刚才提示过期的工程图文件,点击更新视图,下次再打开的时候就不会提 ...