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. Vue-router(4)之路由跳转

    路由传参 案例:现在需要展示一个电影列表页,点击每一部电影,会跳转到该部电影详情页(跳转时携带type和id) 代码实现(未携带type): index.js import Vue from 'vue ...

  2. [软件] Omnigraffle

    一个商业软件, mac下画画图, 还挺好用的. 网上可以找到可用的注册码 https://blog.csdn.net/glw0223/article/details/90736751

  3. css常见符号

    * 通配符使用星号*表示,意思是“所有的” 比如:* { color : red; } 这里就把所有元素的字体设置为红色 缺点: 不过,由于*会匹配所有的元素,这样会影响网页渲染的时间 解决: res ...

  4. Vue.js——2.第一个Vue程序

    代码 <div id="app"> <p>{{msg}}</p> </div> <script> let vm=new ...

  5. goweb-动作

    go-模板引擎 动作 Go 模板的动作就是一些嵌入到模板里面的命令,这些命令在模板中需要放到两个 大括号里{{ 动作 }},之前我们已经用过一个很重要的动作:点(.),它代表了传递给模 板的数据.下面 ...

  6. android studio 修改新建EmptyActivity默认布局

    https://www.jianshu.com/p/d4f201135097 打开你的Android Sudio安装目录,我的为D:\Program Files\Android\Android Stu ...

  7. Java--包密封

    参考:http://blog.csdn.net/zhifeiyu2008/article/details/8829637  http://blog.csdn.net/technerd/article/ ...

  8. html 基础笔记

    html 1,一套规则,浏览器认识的规则 2,开发者: 学习Html规则 开发后台程序: -写Html文件(充当模板的作用) -数据库获取数据,然后替换到html文件的指定位置(web框架) 3,本地 ...

  9. [原]调试实战——使用windbg调试崩溃在ole32!CStdMarshal::DisconnectSrvIPIDs

    原调试debugwindbg崩溃crash 前言 最近程序会不定期崩溃,很是头疼!今晚终于忍无可忍,下决心要干掉它!从之前的几个相关的dump可以猜到是有接口未释放导致的问题,但没有确认到底是哪个接口 ...

  10. Java之异常的处理(try-catch)

    import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java. ...