LeetCode 61. Rotate List
来源:互联网 发布:php进销存管理系统 编辑:程序博客网 时间:2024/03/29 15:41
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
.
answer:
class Solution {public: ListNode* rotateRight(ListNode* head, int k) { if(k == 0 ) return head; ListNode * pre = head, * end = head; if(end == NULL || end->next == NULL) return head; int length = 1; while(k > 0){ while(end->next != NULL){ pre = end; end = end->next; length ++; } cout << length; if(k > length) k = k % length; if(k != 0){ end->next = head; pre->next = NULL; head = end; } k --; } 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
- Java数据结构总结
- LeetCode 64 --- Minimum Path Sum
- Codeforces 509E 想法
- QGis依赖的第三方库和工具简介
- 环信SDK 头像、昵称、表情自定义和群聊设置的实现 一(附源码)
- LeetCode 61. Rotate List
- 【OpenCV学习笔记】三、操作像素
- oracle交换分区swap不足
- SpringMVC之Ambiguous mapping(模棱两可的映射)
- QGis的源代码下载以及windows平台使用脚本自动化编译
- 移动前端不得不了解的HTML5 head 头标签(2016最新版)
- react
- cvpr读书笔记[1]:VJ人脸检测框架。Viola-Jones Objects detection framwork
- Design: FloatingActionButton相关的开源库