leetcode 61: Rotate List
2015-08-02 15:41
316 查看
/** * 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) { ListNode* curr1=head; if(curr1==NULL||curr1->next==NULL) return head; int size=1; while(curr1->next) { curr1=curr1->next; size++; } k%=size; while(k--) { ListNode* curr2=head; while(curr2->next!=curr1) curr2=curr2->next; curr2->next=NULL; curr1->next=head; head=curr1; curr1=curr2; } return head; } };
相关文章推荐
- NSString基本用法
- [poj 2960]S-Nim
- Font: a C++ class
- 用栈实现队列-用队列实现栈
- android record
- 小贝_redis list类型学习
- 编写struts.xml时的提示问题
- TI FAE 中文论坛
- DesignPattern之Mediator
- 感受异或的神奇
- 通过 VMware 安装 Mac OS X 10.10 Yosemite 8
- [深入理解Android卷一全文-第四章]深入理解zygote
- Lining Up Rabbit hunt poj 1118 poj 2606 点共线问题。
- php中如何防止CSRF攻击
- Objective-C学习笔记_继承、初始化方法、便利构造器
- Bitmap: a C++ class
- java 集合类结构图 List Set Map 对比
- greenlet代码解读
- Creating an AVI in memory with C++
- ajax 传值,Ajax: Asynchoronous Javascript and xml (异步的js和xml). 异步刷新,异步传递.替代表单提交数据,回调函数处理返回的数据