题目:

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.

分类:LinkedList   TwoPointers

代码:关键构造一个环再从相应位置断开

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head)
return head;
int length = ;
ListNode *newHead, *tail;
newHead = tail = head;
//得到结点数
while(tail->next)
{
tail = tail->next;
length++;
}
//构造一个换
tail->next = head; if(k %= length)
{
for(int i = ; i < length - k; ++i)
tail = tail->next;
}
newHead = tail->next;
tail->next = NULL;
return newHead;
}
};

[LeetCode61]Rotate List的更多相关文章

  1. Leetcode61. Rotate List旋转链表

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  2. [Swift]LeetCode61. 旋转链表 | Rotate List

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  3. Canvas绘图之平移translate、旋转rotate、缩放scale

    画布操作介绍 画布绘图的环境通过translate(),scale(),rotate(), setTransform()和transform()来改变,它们会对画布的变换矩阵产生影响. 函数 方法 描 ...

  4. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  5. [LeetCode] 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] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  7. jQuery.rotate.js参数

    CSS3 提供了多种变形效果,比如矩阵变形.位移.缩放.旋转和倾斜等等,让页面更加生动活泼有趣,不再一动不动.然后 IE10 以下版本的浏览器不支持 CSS3 变形,虽然 IE 有私有属性滤镜(fil ...

  8. CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)

    CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)   在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾 ...

  9. 偏移:translate ,旋转:rotate,缩放 scale,不知道什么东东:lineCap 实例

    <!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...

随机推荐

  1. MVC数组模型绑定

    ASP.NET MVC数组模型绑定   在ASP.NET MVC中使用Razor语法可以在视图中方便地展示数组,如果要进行数组模型绑定,会遇到索引断裂问题,如下示例: <input type=& ...

  2. stripslashes和addslashes的区别

    我们在向mysql写入数据时,比如: mysql_query(”update table set `title`=’kuhanzhu’s blog’”); 那就会出错.同asp时一样,数据库都会对单引 ...

  3. hdu1690 Bus System (dijkstra)

    Problem Description Because of the huge population of China, public transportation is very important ...

  4. hadoop每个家庭成员

    本文没有提到的原则.谈论hadoop项目周边,它的作用. hadoop这个词已经流行了很多年.大数据的记载会认为hadoop,然后hadoop的作用是什么呢? 官方定义:hadoop是一个开发和执行处 ...

  5. commons.net.telnet使用示例

    import org.apache.commons.net.telnet.TelnetClient; import java.io.IOException; public class TelnetDe ...

  6. Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式

    Eclipse 改动凝视的 date time 日期时间格式,即${date}变量格式 找到eclipse安装文件夹以下的plugins文件夹,搜索 org.eclipse.text ,找到一个jar ...

  7. 利用Java针对MySql封装的jdbc框架类 JdbcUtils 完整实现(包括增删改查、JavaBean反射原理,附源代码)

    近期看老罗的视频,跟着完毕了利用Java操作MySql数据库的一个框架类JdbcUtils.java,完毕对数据库的增删改查.当中查询这块,包含普通的查询和利用反射完毕的查询,主要包含以下几个函数接口 ...

  8. JS正则验证邮箱的格式

    一.相关的代码  1  function test()  2         {  3            var temp = document.getElementById("text ...

  9. php 禁止 URL 直接访问 php文件

    通过判断访问来源来实现. $fromurl="http://www.111.com/index.php"; //只能从这个地址访问 if( $_SERVER['HTTP_REFER ...

  10. Git在下搭建下环境的工具

    (本文稿来自:http://www.open-open.com/news/view/55387) Git是一个快速,可扩展的,分布式的版本控制系统.Git服务器起初是专为Linux开发,后来移植至Wi ...