题目:

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. (一)使用log4net生成日志文件

    1.引入log4net.dll 1.1 Nuget安装 或 http://logging.apache.org/log4net/下载log4net的源代码,编译后把log4net.dll引入项目. 2 ...

  2. Linux /dev 自动创建设备节点

    #include <linux/module.h> #include <linux/module.h> #include <linux/kernel.h> #inc ...

  3. 一些 PHP 管理系统程序中的后门

    一些php网站管理程序的,一些后门,其实官方也没有恶意,主要是大家为了自己的安全. 我倒不怎么关心提示框,SABLOG怎么知道我的版本有漏洞呢,程序肯定有后门.每次登陆后台自动检测官方版本跟当前版本对 ...

  4. Newtonsoft.Json同时对多个时间字段以不同的格式序列化

    在博客园潜水多年,学到很多,也进步了很多,在这里说声谢谢,是时候给园友分享一点自己的东西,希望和大家一起进步. 之前有个需求要对一张表的多个时间字段进行不同的格式序列化, 在网上没找到相对较好的解决方 ...

  5. Jboss wildfly add JDBC driver

    Jboss wildfly  添加 JDBC driver 我这里使用的是 wildfly-8.0.0.Final 第一步: 首先在modules里面添加mysql的驱动包 例如:modules\sy ...

  6. Unity3D实现立体迷宫寻宝

    Unity3D实现立体迷宫寻宝 这个小游戏是一个白痴在一个昏暗的房间走动找到关键得分点,然后通关游戏.入门Unity3D做的第一款游戏,比较无聊,但实现了一般的游戏功能.如,人物控制,碰撞检测,主控制 ...

  7. 微软职位内部推荐-SDE2 (Windows driver)

    微软近期Open的职位: SDE2 (Windows driver) Job title: Software Development Engineer 2 Location: Shanghai, Ch ...

  8. cocos2dx中的用户数据的管理

    提供了专门的类:CCUserDefault用来管理,且提供了单例方法:sharedUserDefault() 1.会在默认路径cocos2d-x-2.2.3\projects\Hello\proj.w ...

  9. getHeight returns 0 for all Android UI objects

    It's 0 because in both onCreate and onStart, the view hasn't actually been drawn yet. You can get ar ...

  10. 复习linq

    复习linq linq的英文是language integrated query.其中query的意思就是疑问或者计算机用语就是从资料库中提取信息的要求,可以理解为查询的意思.那么它翻译过来的话就是集 ...