leetcode 61. Rotate List
来源:互联网 发布:淘宝商家阶层 编辑:程序博客网 时间:2024/04/29 06:21
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
.
class Solution { public: ListNode* rotateRight(ListNode* head, int k) { if (k == 0) return head; ListNode*p = head; int len = 0; while (p != NULL) { p = p->next; len++; } if (len == 0||len==1) return head; k = k%len; if (k == 0) return head; p = head; int kk = 1; while (kk != len - k) { p = p->next; kk++; } ListNode*p1 = p->next, *p2 = p->next; while (p1->next != NULL) p1 = p1->next; p1->next = head; p->next = NULL; head = p2; return head; } };
accepted
0 0
- [LeetCode]61.Rotate List
- LeetCode --- 61. Rotate List
- [Leetcode] 61. Rotate List
- [leetcode] 61.Rotate List
- [leetcode] 61. Rotate List
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- [leetcode] 61. Rotate List
- 61. Rotate List LeetCode
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- leetcode 61. Rotate List
- [LeetCode]61. Rotate List
- LeetCode 61. Rotate List
- LeetCode *** 61. Rotate List
- LeetCode 61. Rotate List
- [Leetcode]61. Rotate List
- LeetCode - 61. Rotate List
- 工作一年记
- 每日一算法---寻找丑数
- 第三周项目1:求最大公约数
- LeetCode-77-Combinations(回溯法)-Medium
- Tomcat集群配置
- leetcode 61. Rotate List
- MySQL 01 通过二进制包安装MySQL
- UVA 10870 Recurrences(构造矩阵和快速幂)
- BP反向传播算法是如何工作的How the backpropagation algorithm works
- 解决YUM下Loaded plugins: fastestmirror Determining fastest mirrors 的问题
- 欢迎使用CSDN-markdown编辑器
- OpenCV实践之路——Python的安装和使用
- 最大子序列问题
- Dagger 笔记 一