[Leetcode]Rotate List
来源:互联网 发布:矩阵的秩怎么计算 编辑:程序博客网 时间:2024/06/06 16:40
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: /*algorithm fast slower two pointer */ ListNode* rotateRight(ListNode* head, int k) { if(!head || !head->next)return head; int count = 0; for(ListNode* p = head;p;p = p->next)++count; k = k%count; if(k==0)return head; ListNode* f = head,*s = f; //f move k step firstly while(k--)f = f->next; while(f->next){ f = f->next; s = s->next; } //f point to last,s point k ahead of end ListNode* nh = s->next; s->next = NULL; f->next = head; return nh; }};
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
- 过滤数组中不想要的元素
- 对Cookie的理解
- python封装screenrecord成一个录屏工具,并输出gif图
- responsrializer-afn
- Deep Learning(深度学习)学习笔记整理系列之常用模型
- [Leetcode]Rotate List
- oc的增 删 改 总结
- c++ primer第五版(中文)习题答案 第三章第五节-数组
- 05 集群安装(stand alone)
- ViewPager中切换界面Fragment被销毁解析
- WITH子句定义一张临时表
- HashMap
- linux——Shell的控制结构(附shell编写代码和运行结果)
- JDK数据类型Short、Long