LeetCode 61. Rotate List
来源:互联网 发布:hypermill软件 编辑:程序博客网 时间:2024/05/17 21:55
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 == NULL) { return head; } ListNode* newHead = head; ListNode* cur = head; ListNode* last; int n = 0; while(cur != NULL) { last = cur; cur = cur->next; ++n; } k = k % n; if (k == 0) { return head; } ListNode* pre; for (int i = n; i > k; --i) { pre = newHead; newHead = newHead->next; } pre->next = NULL; last->next = head; return newHead; }};
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
- springmvc整合freemarker教程
- jvm配置(据说是阿里的)
- windows网络连接不上,网关为0.0.0.0的解决办法
- 猴子选大王,有1-100个数字,每数6位删除一个,顺序循环,最后只有一个数字的时候是1-100中的几?
- array_merge和+区别php
- LeetCode 61. Rotate List
- U812.0凭证冲销后前台不能删除问题
- 泛型Map的遍历
- 【Java JVM】Java虚拟机内存分配几个原则
- Javascript中常遇到的浏览器兼容问题和解决方法
- 《如何阅读一本书》笔记
- 从失望中走向学业的顺利完成,并顺利实现工作的确定---我的2016
- View相关知识
- 2016.12.21_IAR头文件和文件路径的问题