Rotate List
来源:互联网 发布:淘宝怎么用订单号查询 编辑:程序博客网 时间:2024/06/13 02:13
-----QUESTION-----
Given a list, rotate the list to the right by
For example:
Given 1->2->3->4->5->NULL
2
,
return 4->5->1->2->3->NULL
.
-----SOLUTION-----
class Solution {public: ListNode *rotateRight(ListNode *head, int k) { if(!head) return head; ListNode *p = head; ListNode *newHead; int num = 1; while(p->next) { p = p->next; num++; } k = k%num; if(k==0) return head; p->next = head; p = head; for(int i = 0; i<num-k-1; i++) { p = p->next; } head = p->next; p->next = NULL; return head; }};
0 0
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- rotate-list
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- brew软件包管理利器
- mac version破解方法
- Python REST API 框架
- job相关脚本
- cocos2.2通用的消息提示框
- Rotate List
- 2015阿里巴巴笔试题第二题
- DragDrop 事件不响应的问题
- Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure t
- 恰年网络科技:引发苏州企业建站新革命
- linux平台开发自学笔记(二)-打造IDE
- OC 成员变量与方法
- 判断字符串是否不为空,包括判断null/undefined
- android 进度加载条