【Rotate List】cpp
题目:
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 || !(head->next)) return head;
ListNode *p = head;
int len = ;
for (;p->next;++len,p=p->next){}
ListNode *end = p;
k = k % len;
p = head;
for (size_t i = ; i < len-k-; ++i)
{
p = p->next;
}
end->next = head;
head = p->next;
p->next = NULL;
return head;
}
};
Tips:
思路很简单,需要注意的是对指针的操作。
=====================================
第二次过这道题:
(1)没有考虑k比ListNodes长度大的情况
(2)没考虑k加上双指针的边界情况没有考虑完全,所以几次都没有AC。
完备的思路应该是:先求出来ListNodes的长度,k%len就是真正要rotate的元素。搞清楚这之后,再利用快慢指针常规思路就可以解出来了。
/**
* 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 || !head->next ) return head;
int len = ;
for ( ListNode* p = head; p->next; ++len, p=p->next){}
k = k % len;
ListNode dummpy(-);
dummpy.next = head;
ListNode* p1 = head;
ListNode* p2 = head;
for ( int i=; i<k; ++i ) p2 = p2->next;
while ( p2->next )
{
p1 = p1->next;
p2 = p2->next;
}
p2->next = head;
dummpy.next = p1->next;
p1->next = NULL;
return dummpy.next;
}
};
【Rotate List】cpp的更多相关文章
- 【Rotate Image】cpp
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- leetcode 【Rotate List 】python 实现
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- leetcode 【 Rotate Image 】python 实现
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
随机推荐
- 【Shell脚本学习24】Shell输入输出重定向:Shell Here Document,/dev/null文件
Unix 命令默认从标准输入设备(stdin)获取输入,将结果输出到标准输出设备(stdout)显示.一般情况下,标准输入设备就是键盘,标准输出设备就是终端,即显示器. 输出重定向 命令的输出不仅可以 ...
- IUserStore------Implements Diagram
- spring中用xml配置构造注入的心得
spring中用xml配置构造注入时,如果 <constructor-arg> 属性都是 ref ,则不用理会参数顺序 <constructor-arg ref="kill ...
- git版本管理工具 标签(Tag) / 版本回退 / 分支的简单使用
a.标签 标签,可以使用这个功能来标记发布结点. 举个例子, 假如我们的项目版本目前是1.2版本, 上级要求这个版本要在半个月后再进行上传至Appstore, 并要求我们未来的半个月内,去写1.3版本 ...
- 详解Unity 4.6新UI的布局
本文所讲的是Unity 4.6中新加入的uGUI,官方称Unity UI,而不是过去的OnGUI式的旧UI(官方称Legacy GUI). 我曾经在8月份对照4.6 Beta的文档写过一篇笔记学习Un ...
- IOS CALayer基本使用 (图层)
● 其实UIView之所以能显示在屏幕上,完全是因为它内部的一个图层(CALayer) ● 在创建UIView对象时,UIView内部会自动创建一个图层(即CALayer对象),通过UIView 的l ...
- mongodb索引 全文索引之相似度查询
我们在百度搜索中,可以看到与自己搜索度内容越相关度,排在越前面,这个需求可以在mongodb中很简单度实现,mongodb的全文索引不仅可以返回相匹配的查询结果,而且可以告诉你查询结果与你的查询条件多 ...
- python_71_json序列化1
#序列化:序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程. #本例把字典数据类型存成字符串存在硬盘 #文件只能存字符串和二进制码,字典之类的不可以 info={ ...
- python_60_装饰器3
#嵌套函数 def foo(): print('in the foo') def bar(): print('in the bar') bar() #bar()#出错,无法在外边调用,bar函数的作用 ...
- vue2.0在页面中自定义组件模块,以及页面与组件之间的数据传递
1,在初始文件index.html中加入要引入的模块,注意驼峰命名的方式(我就是没写成驼峰,报错) <!DOCTYPE html> <html> <head> &l ...