[leetcode]Rotate List
来源:互联网 发布:java spring压缩 编辑:程序博客网 时间:2024/06/06 16:32
Rotate List
Difficulty:Medium
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
.
题目就是把链表循环右移k个位置,我是先把首尾相连,然后根据k进行移位找到新的末尾和开头
ListNode* rotateRight(ListNode* head, int k) { if (head == NULL)return NULL;int size = 1;//节点数ListNode *last = head ,*p = head,*newHead = NULL;while (last->next != NULL){//找到末尾和节点数last = last->next;size++;}last->next = head;//头尾连接k = k%size;//取余int nextLast = abs(size - k - 1);//移位数while (nextLast > 0){//右移位找到新的尾部p = p->next;nextLast--;}newHead = p->next;//新的头p->next = NULL;//新的尾return newHead; }
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
- 性能测试计数器分析指南
- 距离银牌只差一个Lucas的距离
- 第一周(1) 创新实训开篇:MyFlag的创意背景及准备工作的介绍
- solr 教程
- Linux之交叉编译概要总结
- [leetcode]Rotate List
- 一张图随时查阅 iOS 各种设备屏幕显示转换
- 自定义View之头部可伸缩的ListView
- 【解决】virtualbox中安装centos7无法联网
- Problem E: 类模板Sample
- PAT_1048. Find Coins
- Problem F: 多少个最大值?
- Lottie初级教程:打造 iOS App 完美动画
- Problem G: 动物要吃饭