[Leetcode]61. Rotate List
来源:互联网 发布:灵魂摆渡3风华绝代知乎 编辑:程序博客网 时间:2024/04/29 05:50
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 == nullptr || k == 0 || head->next == nullptr) return head; ListNode *p = head; int len = 1; for (; p->next != nullptr; p = p->next) ++len; k = len - k % len; p->next = head; for (int i = 0; i != k; ++i) p = p->next; head = p->next; p->next = nullptr; return head; }};
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
- MyBatis缓存配置
- 国军标-Gjb软件设计说明书模板
- android 面试题
- 面向对象整体GIS数据模型的设计与实现
- Python学习-格式化输出
- [Leetcode]61. Rotate List
- HBase shell scan命令中filter的使用
- 服务治理与Dubbo架构
- 【bzoj3998】[TJOI2015]弦论 后缀自动机
- iphone忘记锁屏密码解决办法
- 【iOS开发】如何给字符串添加超连接(响应事件)
- 2016.06.29进程和线程的区别(待完善)
- ubuntu14.04自建局域网源在线安装CDH5.6.0
- 【BZOJ4197】寿司晚宴,状态压缩DP