【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->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的更多相关文章
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】48. Rotate Image
Difficulty:medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/rotate-image/ ...
- 【一天一道LeetCode】#61. Rotate List
一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...
- 【LeetCode】189. Rotate Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 切片 递归 日期 题目地址:https://leet ...
- 【LeetCode】48. Rotate Image 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 【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 ...
- 【LeetCode】048. Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【LEETCODE】61、对leetcode的想法&数组分类,适中级别,题目:162、73
这几天一直再想这样刷题真的有必要么,这种单纯的刷题刷得到尽头么??? 这种出题的的题目是无限的随便百度,要多少题有多少题,那么我这一直刷的意义在哪里??? 最近一直苦苦思考,不明所以,刷题刷得更多的感 ...
随机推荐
- 同网段电脑互ping
两台同网段的主机(host)之间的网络通信是不经过网关的. 今天试了一下,用一根网线连接两台电脑,然后 在一台电脑上设置: ip地址:192.168.0.1 子网掩码:255.255.255.0 在另 ...
- angular popover的触发问题;
popover 一般如下用法; <div uib-popover="内容" popover-animation="false" popover-appen ...
- android源码 分享1
技术博客 "碎片化"严重,安卓走向封闭?[Bus Weekly]48期 阅读数:286 2017-05-08 按照NetMarketShare统计数据显示, ...
- 在Linux环境如何在不解压情况下搜索多个zip包中匹配的字符串内容
今天有个生产文件需要查日志,但因为是比较久远的故障,日志已经被归档为zip包放到某个目录下了,在不知道具体日期时间的情况下,总不能一个一个解压搜索吧.于是就研究一下怎么在多个压缩包里搜索字符串了.目前 ...
- jsp 按钮颜色
jsp 按钮颜色 第一种方法 <input style= "color:#FF0000;background-color:#00FF00;" type="butto ...
- 常用的一些js和css
/*给一组li里面写入12345.....*/ $("li").html(function(idx){ return idx+1; }) css限制文字字数: white-spac ...
- Js中的数据属性和访问器属性
Js中的数据属性和访问器属性 在javaScript中,对象的属性分为两种类型:数据属性和访问器属性. 一.数据属性 1.数据属性:它包含的是一个数据值的位置,在这可以对数据值进行读写. 2.数据属性 ...
- .net 中的相等性比较
引用相等性和值相等性 在 C# 中,相等性分为引用相等性和值相等性.引用相等性是指,若两个引用类型的变量引用的是同一个对象,则它们具有引用相等性. // x, y, z 都是引用类型变量 object ...
- 用 volume container 共享数据 - 每天5分钟玩转 Docker 容器技术(42)
volume container 是专门为其他容器提供 volume 的容器.它提供的卷可以是 bind mount,也可以是 docker managed volume.下面我们创建一个 volum ...
- 20170711_map/reduce
js: map: var arr = [1,2,3,4,5]; var res = arr.map(function(x){ return x * x; }); //res 现在 = [1,4,9,1 ...