原题地址

我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊。比如:

1->2->3->4->5->NULL

向右旋转2的距离,变成了:

4->5->1->2->3->NULL

后来琢磨半天+跟人讨论,似乎可以这么理解"rotate"

1. 循环shift。这个比较容易理解。

2. 环旋转。意思是把list首尾连接成一个环进行旋转。想象有一桌菜,你坐在桌边,原先list头部的那盘菜现在就在你眼前,你可以伸手去转桌上的转盘。比如:

然后,你把桌上的菜向右转了两个单位,变成这样:

最后,你沿着顺时针方向依次吃完了整桌菜,你吃菜的顺序是什么?4->5->1->2->3->NULL,哇,正式我们想要的结果。

代码:

 ListNode *rotateRight(ListNode *head, int k) {
if (!head) return head; int n = ;
ListNode *h = head; while (h->next) {
h = h->next;
n++;
}
h->next = head; n = n - (k % n);
while (n--) {
head = head->next;
h = h->next;
}
h->next = NULL; return head;
}

Leetcode#61 Rotate List的更多相关文章

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

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

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

  4. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  5. leetCode 61.Rotate List (旋转链表) 解题思路和方法

    Rotate List  Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...

  6. [leetcode]61. Rotate List反转链表k个节点

    类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null ...

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

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

  8. [array] leetcode - 48. Rotate Image - Medium

    leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...

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

随机推荐

  1. C# MongoDB--时区问题(差了8小时)

    原因:MongoDB中存储的时间是标准时间UTC +0:00C#的驱动支持一个特性,将实体的时间属性上添加上这个特性并指时区就可以了.例如:[BsonDateTimeOptions(Kind = Da ...

  2. Java 中的抽象类及接口

    抽象类使用 abstract 关键字修饰,该类即为抽象类. 抽象类的作用: 1.某些情况下,父类约束子类必须包含哪些方法,但不知道子类如何去实现这些方法. 2.可以从多个具有相同特征的类中抽象出一个抽 ...

  3. php中关于抽象(abstract)类和抽象方法的问题解析

    在面向对象(OOP)语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法作为外部代码访问的接口.而抽象方法就是为了方便继承而引入的,现在来看一下抽象类和抽象方法分别是如何定义以及他们的特点 ...

  4. Nginx配置:http重定向,URLRewrite,一个简单框架的配置思路

    一个重定向的应用配置: server { listen       8000; server_name  localhost; root F:/home/projects/test; index   ...

  5. Delphi 的运算符列表

    分类 运算符 操作 操作数 结果类型 范例 算术运算符 + 加 整数,实数 整数,实数 X + Y - 减 整数,实数 整数,实数 Result - 1 * 乘 整数,实数 整数,实数 P * Int ...

  6. 【转载】input 中 type='text' 的提交问题

    原文链接:http://www.nowamagic.net/html/html_AboutInputSummit.php 有时候我们希望回车键敲在文本框(input element)里来提交表单(fo ...

  7. UIViewAnimationOptions swift 2

    UIView.animateWithDuration(0.5, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, ...

  8. wpf 动画 2个窗体切换

    <Window x:Class="翻转.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xam ...

  9. 界面控件 - 滚动条ScrollBar

    界面是人机交互的门户,对产品至关重要.在界面开发中只有想不到没有做不到的,有好的想法,当然要尝试着做出来.对滚动条的扩展,现在有很多类是的例子. VS2015的代码编辑是非常强大的,其中有一个功能可以 ...

  10. 【转】Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败

    错误原因如下: Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot ...