LeetCode61——Rotate List
来源:互联网 发布:珠江新闻眼回看软件 编辑:程序博客网 时间:2024/06/05 20:31
LeetCode61——Rotate List
简单的链表操作。维护head和r指针,r初始指向链表末尾,并且r-next=head;
首尾相接,再根据给定K移动特定指针。
比如说,链表长度len=5,那么当k为1时,head指针向后移动len-k=4步,而r指针也移动同样步骤,这样始终有r->next==head;
最后将循环链表拆开为单链表即可。
注:根据提交经验,k会给很大,这时我们让k对链表长度取模即可。
代码:
class Solution {public:ListNode* rotateRight(ListNode* head, int k) {if (k == 0 || head == NULL)return head;ListNode*r = head;int count = 1;while (r->next){count++;//链表长度r = r->next;}k = k%count;//处理kif (k==0){return head;}r->next = head;//循环链表int move = count - k;while (move--){head = head->next;r = r->next;}r->next = NULL;//拆开return head;}};
后记:
今天做了11题,累坏了。。。写完博客洗洗睡了、、
0 0
- LeetCode61——Rotate List
- Leetcode61 Rotate List
- leetCode61:Rotate List
- leetcode61 Rotate List
- leetcode61: Rotate List
- Leetcode61 rotate list
- leetcode61~Rotate List
- LeetCode——Rotate List
- Algorithms—61.Rotate List
- leetcode61
- leetcode 061 —— Rotate List
- LeetCode题解——Rotate List
- LeetCode OJ——Rotate List
- Leetcode题集——rotate-list
- Leetcode——61. Rotate List
- 算法系列——Rotate List
- 2.2.6—单链表—Rotate List
- LeetCode]—Rotate List 循环右移链表
- 面试题
- 1018. 锤子剪刀布 (20)
- UFT在飞机订票系统中的应用
- cocos2d-lua集成Mob-SMSSDK-for-android
- SQLite支持的数据类型(日期函数)
- LeetCode61——Rotate List
- linux下的tar解压命令参数详解
- 复习测试基础
- 动态规划——最大子段和
- activity生命周期深入理解
- C++在命令行中回车实现默认输入
- HTTP协议原理分析
- Android应用程序Manifest文件简介
- 【鸟哥的linux私房菜-学习笔记】文件与目录管理