题目:

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.

链接:http://leetcode.com/problems/rotate-list/

题解:

先遍历一次链表,将尾部和头部相连,再进行移动。注意右移k步相当于prehead travel len - k步。

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0)
return head;
ListNode node = head;
int len = 1;
while(node.next != null){
node = node.next;
len ++;
}
node.next = head;
k %= len;
for(int i = 0; i < len - k; i++)
node = node.next; head = node.next;
node.next = null;
return head;
}
}

二刷:

主要思路还是把链表结成环。

  1. 先求出链表长度。
  2. 将链表结成环
  3. 计算k的合理范围。向右移动k位,就相当于从表头向左travel len - k个单位: 
    1. 假如k < 0,那么说明向左移动,我们只需要计算-k % len就可以了
    2. 假如k > 0,我们需要把k变换为 len - k % len
  4. 接下来当k > 0的时候,我们从node继续往下travel, k--
  5. 第4步的遍历结束时,我们可以设置新的head = node.next,然后在此处断开环, node.next = null
  6. 最后返回head。

Java:

Time Complexity - O(n), Space Complexity - O(1)

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
ListNode node = head;
int len = 1;
while (node.next != null) {
node = node.next;
len++;
}
node.next = head;
if (k < 0) {
k = -k % len;
} else {
k = len - k % len;
}
while (k > 0) {
node = node.next;
k--;
}
head = node.next;
node.next = null;
return head;
}
}

三刷:

看到k是non-negative的我就放心了。这里要注意我们结成环以后,在链表尾部寻找新的表头,需要移动的距离是  len - k % len。这样node.next就是新的表头。

Java:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || k <= 0) return head;
ListNode node = head;
int len = 1;
while (node.next != null) {
node = node.next;
len++;
}
node.next = head; k = len - k % len;
while (k > 0) {
node = node.next;
k--;
}
head = node.next;
node.next = null;
return head;
}
}

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. [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 ...

  5. 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 ...

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

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

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

  8. [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 ...

  9. 61. Rotate List(List)

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

随机推荐

  1. 使用Animation实现Button的透明度Opacity变化

    接着之前的使Button的Content变化的例子,这里给出使Button的透明度变化的写法. 前台写法: 后台写法: 效果图:Opacity的值正在变小 效果还不错,前台是用Blend生成的,后台的 ...

  2. GoldenGate单向复制配置示例

    一:环境介绍 --source端 ip地址:192.168.123.10 数据库版本:11.2.0.1.0 32 bit 操作系统版本:centos 4.5 32 bit ogg版本:fbo_ggs_ ...

  3. C# 枚举,传入int值返回string值

    需求:1:子公司负责人2:人事3:审批人4:签批人 5:管理员  传入值为1,2,3,4,5这个数字的某一个.需要返回他们的中文描述. 一下忘记该怎么写了...后来百度下查出来了..记录下当个小工具吧 ...

  4. ASP.NET对HTML元素进行权限控制(二)

    这是这个权限控制的第一步,扫描界面把要分配权限的元素的信息获取出来存入到数据库中. 这一步分三小步: (1).标出界面所要分配权限的元素 (2).扫描界面获取所要分配权限的元素信息.(ID,标题,层级 ...

  5. SQLserver利用系统时间生成“2015-11-30 00:00:00.000”类型的时间

    select getdate() ---当前时间:2015-12-18 10:20:24.097 -------------------建立测试表 Create Table #Test ( ID IN ...

  6. lnmp停用nginx,改用apache

    编译安装的lnmp环境 总是出现502错误,修改了各种配置也没用,暂时先放弃nginx,改用apache apache使用yum安装方式 需要注意的事项,将网站根目录的用户组改为 chown apac ...

  7. 在myeclipse中使用Java语言进行spark Standalone模式应用程序开发

    一.环境配置 Myeclipse中虽然已经集成了maven插件,但是由于这个插件版本较低,建立maven project会出现错误. 解决办法:自己到官网http://maven.apache.org ...

  8. java集合类(五)About Map

    接上篇“java集合类(四)About Set” 这次学完Map之后,就剩队列的知识,之后有关java集合类的学习就将告一段落,之后可能会有java连接数据库,I/O,多线程,网络编程或Android ...

  9. ffmpeg 编码

    编码可以简单理解为将连续的图片帧转变成视频流的过程.以H264为例给出编码的代码: int InitEncoderCodec(int width, int height) { auto enc = a ...

  10. knockoutjs select onchange 下拉级联

    1.绑定数据源 <select name="" class="xlb02" data-bind="options: $root.dataSour ...