【LeetCode】 Rotate List
来源:互联网 发布:angularjs js cdn 编辑:程序博客网 时间:2024/06/10 05:42
Rotate List
Total Accepted: 13773 Total Submissions: 62790My SubmissionsGiven 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 || head->next == NULL) return head; if (k <= 0) return head; int len = 0; ListNode *p = head, *tail = head; while (p) { len++; tail = p; p = p->next; } k = k % len; if (k == 0) return head; int i = 0; p = head; while (p) { i++; if (i == len - k) break; p = p->next; } ListNode *tmp_head = p->next; p->next = NULL; tail->next = head; return tmp_head; }};
0 0
- [leetcode][list] Rotate List
- LeetCode: Rotate List
- LeetCode Rotate List
- LeetCode : Rotate List
- [Leetcode] Rotate List
- [LeetCode] Rotate List
- [leetcode] Rotate List
- leetcode 70: Rotate List
- [Leetcode] Rotate List
- 【leetcode】Rotate List
- [LeetCode]Rotate List
- [leetcode]Rotate List
- LeetCode-Rotate List
- [leetcode] Rotate List
- LeetCode - Rotate List
- LeetCode:Rotate List
- LeetCode 61: Rotate List
- leetcode之Rotate List
- fork与vfork的区别
- 《硅谷传奇》短评
- 基于C#.net技术的数据安全删除系统的研究与实现
- Attribute is missing the Android namespace prefix问题解决
- NYOJ 2 括号配对问题
- 【LeetCode】 Rotate List
- 将session的值存储到memcached中去,配置以及方法和注意事项
- Java GC 日志分析
- 按时间回到过去有他速度快
- Double Profiles - CodeForces 154C 哈希
- jquery对表单元素的取值和赋值
- POJ 2421 Constructing Roads
- 【专题】—【数据类型】—【long long 与 __int64 的区别】
- 关于研究生面试系统的探索研究V