题目:

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. Eclipse 未开始 【Ubuntu】

    /usr/lib/eclipse/configuration/1408532831122.log : !SESSION 2014-08-20 19:07:11.055 ---------------- ...

  2. Ajaxterm-0.10-8.el5.noarch.rpm CentOS 5 (RHEL 5) Download

    Ajaxterm-0.10-8.el5.noarch.rpm CentOS 5 (RHEL 5) Download Install Howto Download the latest epel-rel ...

  3. FluentData

    FluentData微型ORM 最近在帮朋友做一个简单管理系统,因为笔者够懒,但是使用过的NHibernate用来做这中项目又太不实际了,索性百度了微型ORM,FluentData是第一个跳入我眼睛的 ...

  4. JS类定义方式

    // 方法1 对象直接量 var obj1 = { v1 : "", get_v1 : function() { return this.v1; }, set_v1 : funct ...

  5. Jetty开发指导:Jetty Websocket API

    Jetty WebSocket API使用 Jetty提供了功能更强的WebSocket API,使用一个公共的核心API供WebSockets的服务端和client使用. 他是一个基于WebSock ...

  6. 使用SharePoint管理中心管理服务

    使用SharePoint管理中心管理服务 为了管理服务应用程序.场管理员要么使用管理中心,要么使用PowerShell. 管理服务应用程序页面列出了场上执行的服务.你能够管理他们. 很多服务都有自己的 ...

  7. C++经典面试题(二)

    近期看一些面试题,认为假设自己被问到了,并不能非常利落的回答出来. 一是从来没有这个意识,二是没有认真的梳理下. 以下对这些题做出分析,哈!个人能力有限,当中难免有疏漏,欢迎大家补充咯. 11.引用与 ...

  8. Windows Phone开发(14):数据模板

    原文:Windows Phone开发(14):数据模板 数据模板,如果你仅仅听到这个名词,你一定很迷惑,什么来的?用来干什么的?不急,亲,今天,我们一起来探索一下吧. 用白话文说,数据模板就是用来规范 ...

  9. Composite Design Pattern 设计模式组合

    设计模式组合,它能够更类组合在一类,形成一个树状结构. #include <set> #include <iostream> #include <string> u ...

  10. Golang在Linux环境下的POSIX风格socket编程

    这里给出一个服务端和client,服务端能够接受多个连接,而且利用Go的杀手特性go和channel来替代select进行数据的接收. 服务端: package main import ( " ...