Leetcode#61 Rotate List
我一直不太理解为什么叫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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- 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 ...
- [leetcode]61. Rotate List反转链表k个节点
类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- 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. ...
随机推荐
- linux expect初识
写个命令,让ssh服务器便捷点 #!/usr/bin/expect set type [lindex $argv 0] if {$type == "server"} { set i ...
- Java迷题:等于,还是不等于?
等于还是不等于? 看来看下面的一段代码: public static void main(final String[] args) { Integer a = new Integer(100); In ...
- 使用mitmf 来绕过HSTS站点抓取登陆明文
使用mitmf 来绕过HSTS站点抓取登陆明文 HSTS简介 HSTS是HTTP Strict Transport Security的缩写,即:"HTTP严格安全传输".当浏览器第 ...
- 推荐个好东西swoole,php如虎添翼
Swoole:PHP语言的异步.并行.高性能网络通信框架,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列 ...
- delphi 基础之三 编写和调用dll文件
delphi 编写和调用dll文件 Windows 的执行文件可以划分为两种形式程序和动态连接库 (DLLs).一般程序运行是用.EXE文件,但应用程序有时也可以调用存储在DLL的函数. 在如下几 ...
- DevExpress 表中数据导出
gridView1.ExportToXlsx("SampleStock.xlsx"); if (true) { DevExpress.XtraEditors.XtraMessage ...
- 会写网页 就会写手机APP -- Hybrid Mobile Apps for ASP.NET Developers
您好,这篇文章是我的BLOG发出,原始出处在此: 会写网页 就会写手机APP -- Hybrid Mobile Apps for ASP.NET Developers http://www.dotbl ...
- [转]Openwrt的Inittab
转来一篇关于启动的文章,特意收藏.http://see.sl088.com/wiki/Inittab 文件位于/etc/inittab编辑方法vi /etc/inittab初始内容::sysinit: ...
- 与谷歌测试工程师的对话 - from Google Testing Blog
Conversation with a Test Engineer by Alan Faulner Alan Faulner谷歌的一名测试工程师,他工作在DoubleClick Bid Manager ...
- 避免url传值字符串sjstr过长,使用from表单【隐藏域】post提交
1.普通的url传值<html--------------- <!-- 隐藏域post提交url --> <form id="urlPost" action ...