LeetCode 61. Rotate List
来源:互联网 发布:java for循环 编辑:程序博客网 时间:2024/04/28 17:26
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
.
Subscribe to see which companies asked this question
/** * 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
- numpy.random.shuffle打乱顺序函数
- MongoDB 学习三(数据库的增删改查)
- LeetCode 82. Remove Duplicates from Sorted List II
- 图解符号链接和硬链接的区别和i节点的介绍
- Python3 基础:面向对象编程(上)
- LeetCode 61. Rotate List
- 认识TexturePacker的界面
- 手机微信目录
- 闪回数据归档实验(Flashback Data Archive)
- c语言学习100例(1-5)
- 贝叶斯公式中各项的理解
- Java发送邮件
- 缩小的陆地
- 闪回删除实验