原题地址

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

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

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

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

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

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

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

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

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

代码:

  1. ListNode *rotateRight(ListNode *head, int k) {
  2. if (!head) return head;
  3.  
  4. int n = ;
  5. ListNode *h = head;
  6.  
  7. while (h->next) {
  8. h = h->next;
  9. n++;
  10. }
  11. h->next = head;
  12.  
  13. n = n - (k % n);
  14. while (n--) {
  15. head = head->next;
  16. h = h->next;
  17. }
  18. h->next = NULL;
  19.  
  20. return head;
  21. }

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. CentOS下安装LNMP(LINUX+NGINX+MYSQL+PHP)环境

    一.安装Nginx最新版 首先查看是否有安装源包 yum list nginx  (或yum info nginx) 如果没有则 vi /etc/yum.repos.d/nginx.repo #添加如 ...

  2. Yii整合ZF2及soap实例

    一)如何整合? // change the following paths if necessary $yii = dirname(__FILE__).'/framework/yii.php'; $c ...

  3. Laravel 5 基础(九)- 表单

    首先让我们修改路由,能够增加一个文章的发布. Route::get('articles/create', 'ArticlesController@create'); 然后修改控制器 public fu ...

  4. DevExpress BarManager 部分用法

    1.创建一个BarManager会默认产生三个菜单.BarManager右键ShowDesignTime enhancements会显示[add]按钮,可对菜单进行编辑. 2.其中比较有用的属性: 选 ...

  5. C#读取和写入配置文件

    使用.Net2.0中的ConfigurationManager可以方便的实现对配置app.config的读取和写入. ConfigurationManager默认没有自动载入项目,使用前必须手动添加, ...

  6. Android WebView代理设置方法(API10~21适用)

    最近碰到个需求需要在APP中加入代理,HttpClient的代理好解决,但是WebView碰到些问题,然后找到个API10~API21都通用的类,需要用的同学自己看吧,使用方法,直接调用类方法setP ...

  7. hdu1007

    Have you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some ...

  8. Nginx源码结构

    上一章对Nginx的架构有了一个初步的了解.这章,为了对源码仔细的剖析,先要对Nginx的源码结构有一个了解.从宏观上把握源码模块的结构. 一.nginx源码的3个目录结构 在安装的nginx的目录下 ...

  9. JavaScript 编码风格指南

    A.1  缩进 // 4个空格的层级缩进 if (true) { doSomething(); } A.2  行的长度 // 每行限于80个字符,超出则在运算符后换行,缩进2个层级(8个空格) doS ...

  10. ExtJS FormPanel不执行校验

    经检查问题原因在于使用了 validator 属性. 使用validator属性,必须添加返回值.不添加返回值,就会出现FormPanel不执行校验的问题.