Rotate List
来源:互联网 发布:制作mac安装u盘 编辑:程序博客网 时间:2024/05/29 18:37
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||k==0||!head->next) return head; int length = 0; ListNode *ptr = head, *tail = head; while (ptr) { length++; tail = ptr; ptr = ptr->next; } k %= length; ptr = head; for (int i = 0; i < length - k - 1; i++) ptr = ptr-> next; tail->next = head; head = ptr->next; ptr->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
- Frequent values(预处理加线段树维护最大值)
- JAVA学习之4 强引用,弱引用,软引用 与 JVM
- 引入JSP文件标签的区别
- hdoj 2092 整数解 氺过~
- .net 开发微信公众平台(一)-----认证"成为开发者"
- Rotate List
- 五个解决方案让MongoDB拥有RDBMS的鲁棒性事务
- 用CWinThread派生类创建带有窗口的线程(VC++) http://www.softist.com/programemo/cwinthread/cwinthread.htm
- 水池数目
- 磁盘及文件系统
- Android学习计划表
- C++类构造函数初始化列表
- **********POJ Calendar(不明白为什么WA)
- http响应状态码大全