Given a linked list, rotate the list to the right by k places, where k is non-negative.

Example 1:

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:

Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
ListNode cur = head;
int len = 1;
while (cur.next != null) {
cur = cur.next;
len += 1;
}
cur.next = head;
// use len - k%len
for (int i = 1; i < len - k % len; i++) {
head = head.next;
}
ListNode res = head.next;
head.next = null;
return res;
}
}

[LC] 61. Rotate List的更多相关文章

  1. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

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

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

  3. Leetcode#61 Rotate List

    原题地址 我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊.比如: 1->2->3->4->5->NULL 向右旋转2的距离,变成了 ...

  4. 61. Rotate List

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

  5. [LeetCode] 61. Rotate List 解题思路

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  6. LeetCode OJ 61. Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  7. 【leetcode】61. Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  8. 【一天一道LeetCode】#61. Rotate List

    一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...

  9. [leetcode]61. Rotate List旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

随机推荐

  1. .NET CORE 配置Swagger文档

    1.先通过NuGet安装Swashbuckle.AspNetCore ,支持.NET core,版本是4.0.1,以上版本好像有些功能不支持 2.startup文件里注入swagger,Configu ...

  2. mysql分组和排序操作

    分组.排序操作                                                                                         sele ...

  3. VCPU的解释

    vCPU,顾名思义,是虚拟CPU. 创建虚拟机时,需要配置vCPU资源. 因此vCPU是虚拟机的部件. 因此脱离VM,谈论vCPU是没有意义的. 虚拟化管理系统如何调度vCPU,取决于系统内的虚拟机数 ...

  4. iOS中copy和=的区别

    copy是浅拷贝即指针拷贝,让对象的引用计数加一 在MRC环境下,=只是简单的指针指向,引用对象的引用计数并不会增加,在用局部变量赋值时很容易出现野指针 在ARC环境下,=的效果等同于copy和ret ...

  5. 吴裕雄--天生自然 JAVA开发学习: 循环结构

    public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System ...

  6. Vus the Cossack and Strings(Codeforces Round #571 (Div. 2))(大佬的位运算实在是太强了!)

    C. Vus the Cossack and Strings Vus the Cossack has two binary strings, that is, strings that consist ...

  7. flask汇总

    flask框架 蓝图 随着flask程序越来越复杂,我们需要对程序进行模块化的处理,之前学习过python的模块化管理,于是针对一个简单的flask程序进行模块化处理 Blueprint概念 简单来说 ...

  8. 参考JDK1.8源码,自己写一个类似于ArrayList的动态数组

    1. ArrayList的基本实现原理 ArrayLiST其内部用一个普通数组来存储数据,当此数组不够容纳新添加的元素的时候,则创建一个更大长度的新数组,并将原来数组中的元素复制到新数组中. 2.Ar ...

  9. NOIpDairy

    Day 0 水水比赛 Day 1 写写Dp Part1:Dp基础练习 [HNOI2002]公交车路线 秒切,点数这么少,N这么大,目测O(N)+暴力更新 5min写完 P3842 [TJOI2007] ...

  10. Python Numpy中数据的常用的保存与读取方法

    在经常性读取大量的数值文件时(比如深度学习训练数据),可以考虑现将数据存储为Numpy格式,然后直接使用Numpy去读取,速度相比为转化前快很多. 下面就常用的保存数据到二进制文件和保存数据到文本文件 ...