LeetCode_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.
巨没劲的一道题,当k>length 时,我以为origin list 不动就行,结果好些case过不了,看了别人的代码才知道要 k%= length 真心想不通为什么,也懒得弄这种恶心的东西。面试遇到这种题目直接问面试官这种奇葩情况怎么处理就行。
/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head == NULL || k == 0) return head;
ListNode *p, *q;
p = head;q= p;
while(q &&--k){
q = q->next;
}
if(k >0 || NULL == q) return head;
ListNode *pre = NULL; while(q->next){
pre = p;
p = p->next;
q = q->next;
}
q ->next = head;
head = p;
pre ->next = NULL;
return head; }
};
LeetCode_Rotate List的更多相关文章
- LeetCode_Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
随机推荐
- BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏
3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 51 Solved: 4 ...
- google官方提供的下拉刷新控件SwipeRefreshLayout
摘自:http://www.stormzhang.com/android/2014/03/29/android-swiperefreshlayout/ SwipeRefreshLayout Swipe ...
- Android样式的编写格式
<?xml version="1.0" encoding="utf-8"?> <resources> <style name=&q ...
- 百度系统部 在 北京市海淀区西二旗首创空间大厦 招聘 Python-交付运维系统研发工程师 - 内推网(neitui.Me)
百度系统部 在 北京市海淀区西二旗首创空间大厦 招聘 Python-交付运维系统研发工程师 - 内推网(neitui.Me) 汪肴肴 (wa**@baidu.com) 发布了 Python-交付运维系 ...
- Java向上转型注意事项
继承.接口:Java子类中如果含有父类中不包含的变量与方法,子类对象向上转型时就是丢失这些变量和方法. interface SuperClass{ int i = 2; void f() ; } cl ...
- css(二)
重新排传智的首页!头部和左边的部分完成了! <!doctype html> <html lang="en"> <head> <meta c ...
- squid 代理服务器安装配置
ubuntu16.04 安装squid代理服务器配置 本文参考 http://www.cnblogs.com/newflypig/archive/2012/09/28/2862000.html 1,删 ...
- [原创作品] web项目构建(一)
今天开始,将推出web项目构建教程,与<javascript精髓整理篇>一并更新.敬请关注. 这篇作为这一系列开头,主要讲述web项目的构建技术大全.在众多人看来,web前端开发无非就是写 ...
- Mac phpstorm破解版安装(简单,有效)
如果是公司作为商业用途的,还是希望你能购买正版的,如果是苦逼的穷学生,亦或是我这样的苦逼码农,那就往下看, 之前有个只需要在"License server address"里输入 ...
- Matplotlib下载地址
http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib