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.

 /**
* 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==NULL||head->next==NULL||k==)
return head;
ListNode* bf=head;
ListNode* newhead=head,*end=head;
int length=;
while(end->next!=NULL){
end=end->next;
length++;
}
end=head;
if(k>length){
k=k%length;
}
if(k==length||k==)
return head;
k--;
while(k>&&end->next!=NULL){
end=end->next;
k--;
}
while(end->next!=NULL){
end=end->next;
bf=newhead;
newhead=newhead->next;
} bf->next=NULL;
end->next=head;
return newhead; }
};

【leetcode】61. Rotate List的更多相关文章

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

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

  2. 【LeetCode】48. Rotate Image

    Difficulty:medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...

  3. 【一天一道LeetCode】#61. Rotate List

    一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...

  4. 【LeetCode】189. Rotate Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...

  5. 【LeetCode】48. Rotate Image 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】189 - Rotate Array

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

  7. 【LeetCode】48. Rotate Image (2 solutions)

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

  8. 【LeetCode】048. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  9. 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73

    这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...

随机推荐

  1. C实现dos图文菜单程序实例

      前言 公司一台服务器是novell环境,文件管理是基于dos6.22的,客户端启动需要一个图文菜单.   实现    编程环境:汉化版TC2.0 菜单基本功能:显示提示项.显示dbf中的行情信息. ...

  2. mysql常见的优化方法

    1.选取适当的字段属性.例如,在定义邮政编码这个字段时,如果将其设置为CHAR(255),显然给数据库增加了不必要的空间,甚至使用VARCHAR这种类型也是多余的,因为CHAR(6)就可以很好的完成任 ...

  3. 集合的定义,操作及运算 (Python)

    集合的定义: 集合和列表([  ]) 与  字典 ( {    }) 不同,没有特别的特别的语法格式.可以使用set () 创建. 集合同字典一样是无序的.也是不具有重复性的.因此可以把列表变成集合进 ...

  4. localStorage和sessionStorage的使用方法和一些特性介绍

    本文主要介绍的是localStorage和sessionStorage的使用方法和一些特性,以及一些其他的存储方式的比较.   客服端存储方案包括以下几种:     1.Cookie     2.Us ...

  5. Delphi Screen.DataModuleCount 总是返回 0!Delphi 的 Bug? DataModuleCount = 0

         今天遇到一个很隐蔽的 Delphi 问题,不知做了什么,有一个功能总是不能使用,后来跟踪以下发现是因为 Screen.DataModuleCount 总是返回 0,而程序中一个函数正好要用到 ...

  6. vs2015数据驱动的单元测试

    今天在做测试的时候boss让我这个菜鸟做vs2015下c#的单元测试,并且给了我参考http://www.cnblogs.com/kingmoon/archive/2011/05/13/2045278 ...

  7. Ajax的方法和使用代码

    //1.创建Ajax对象,已经封装好的函数: function createXHR(){ if (typeof XMLHttpRequest != "undefined"){ re ...

  8. c#中Class和Struct使用与性能的区别

    在Unity中很多已经定义为结构体的数据结构 Vector2, Vector3 和 Vector4 Rect Color和Color32 Bounds Touch   1.Class为引用类型,Str ...

  9. Android保存图片到本地相册

    好久没有写东西了.备份下知识吧.免得忘记了 . 首先贴一段代码 --  这个是先生成一个本地的路径,将图片保存到这个文件中,然后扫描下sd卡.让系统相册重新加载下 .缺点就是只能保存到DCIM的文 件 ...

  10. Java工程师书单(初级、中级、高级)

    简介 怎样学习才能从一名Java初级程序员成长为一名合格的架构师,或者说一名合格的架构师应该有怎样的技术知识体系,这是不仅一个刚刚踏入职场的初级程序员也是工作一两年之后开始迷茫的程序员经常会问到的问题 ...