【Leetcode】61. Rotate List - 循环链表
来源:互联网 发布:google 算法面试题 编辑:程序博客网 时间:2024/05/22 12:11
思路:
1. 遍历链表找到结尾,顺便统计链表长度count
2. 将结尾指向head,使链表首位相连,并计算前行长度count=count-k%count
3. 前行count步,当前指针为尾,next为new head
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* rotateRight(struct ListNode* head, int k) { if (head==NULL||head->next==NULL){ return head; } struct ListNode *tail; struct ListNode *p=head; int count=1; int newK; while(p->next!=NULL){ p=p->next; count++; } newK=k%count; p->next=head; count=count-newK; p=head; while(count-->1){ p=p->next; } tail=p; head=p->next; tail->next=NULL; return head;}
0 0
- 【Leetcode】61. Rotate List - 循环链表
- 【LeetCode】 Rotate List 循环链表
- Leetcode #61. Rotate List 循环位移链表 解题报告
- LeetCode-----61. Rotate List(循环旋转链表)
- 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
- 61. Rotate List LeetCode
- leetcode 61. Rotate List
- LeetCode 61. Rotate List
- leetcode 61. Rotate List
- [LeetCode]61. Rotate List
- jsp中引用的jstl 和fmt标签
- Math.round这个方法有坑
- 华为上机题汇总(五)
- AngularJS动态加载模块和依赖
- Android注解框架butterknife基本用法
- 【Leetcode】61. Rotate List - 循环链表
- 网易游戏开发软件笔试题1
- BZOJ4668 冷战
- WebService工作学习心得
- SpringMVC+Spring+Mybatis整合配置文件小记
- java Unicode编码转换为汉字
- light oj 1102 Problem Makes Problem [组合数+逆元]
- 如何调用ecshop的轮播图的图片信息
- TIM的一些配置参数