【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日 ...
 
随机推荐
- 解决win2008下IIS7的HTTP500错误
			
造成500错误常见原因有:ASP语法出错.ACCESS数据库连接语句出错.文件引用与包含路径出错.使用了服务器不支持的组件如FSO等.另外,对于win2008的IIS默认不显示详细出错信息的问题以下就 ...
 - What is pseudopolynomial time? How does it differ from polynomial time?
			
To understand the difference between polynomial time and pseudopolynomial time, we need to start off ...
 - html5(历史管理)
			
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/h ...
 - HDU 2874 Connections between cities(LCA)
			
题目链接 Connections between cities LCA的模板题啦. #include <bits/stdc++.h> using namespace std; #defin ...
 - IntelliJ IDEA ,springboot 2.0 +mybatis 创建和访问数据库
			
环境: JDK8+windows10 步骤 New Module —>Spring Initializr—>next 1  2.  3.web勾选web,sql里面可以不勾,后续添加, ...
 - Java原子类及内部原理
			
一.引入 原子是世界上的最小单位,具有不可分割性.比如 a=0:(a非long和double类型) 这个操作是不可分割的,那么我们说这个操作是原子操作.再比如:a++: 这个操作实际是a = a + ...
 - XSY1659 [HNOI2012]永无乡
			
题面 Description 永无乡包含 n 座岛,编号从 1 到 n. 每座岛都有自己的独一无二的重要度,按照重要度可以将这n座岛排名,名次用 1到n来表示.某些岛之间由巨大的桥连接,通过桥可以从一 ...
 - The Process class relies on proc_open, which is not available on your PHP installation
			
[Symfony\Component\Process\Exception\RuntimeException] The Process class relies on proc_open, which ...
 - ORACLE MOS 翻译
			
http://blog.csdn.net/msdnchina/article/details/53174196
 - Interface Builder中的技巧
			
在我工作中经常会遇到有人吐槽Xcode中的interface builder(以下简称IB)不好用的开发者.在我看来,IB是一个非常棒的可视化开发工具,可以非常快捷的设置UI控件的大部分常用属性.下面 ...