【LeetCode】 Rotate List 循环链表
题目:rotate list
解法1:
<span style="font-size:18px;">/**LeetCode Rotate List:Given a list, rotate the list to the right by k places, where k is non-negative.
* 题目:循环移动链表,等价于将链表从右边数的k个节点移动到表的前方
* 思路:移动倒是简单。重点是要找到链表倒数的k个数,就等价于找到倒数第K+1个数,设置两个指针,先后遍历链表。中间相隔k个数
* 当前面的指针走到最后的一个节点,此时后面的指针指向的就是倒数第k+1个数
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
package javaTrain; public class Train14 {
public ListNode rotateRight(ListNode head, int k) {
ListNode pFast,pSlow,pKnode;
int n = 0; if(head == null || k < 1 ) return head; //注意特殊情况
pFast = head;
pSlow = head;
while(pFast != null){
pFast = pFast.next;
n++;
}
k = k%n; //循环移动,能够转变为求模
if(k == 0) return head; //移动的次数等于自己的长度。等价于本身
pFast = head;
while(k>0 && pFast != null){
pFast = pFast.next;
k--;
}
while(pFast.next != null){
pFast = pFast.next;
pSlow = pSlow.next;
}
pKnode = pSlow.next; //第k+1个节点,次后就是要移到前面的节点了,
pSlow.next = null;
pFast.next = head; //原本最后的节点此时排在头结点之前 return pKnode;
}
}
</span>
解法2:
<span style="font-size:18px;">//法2:将链表连城环,而后从新寻找新的头结点和尾节点,即在len-k处
package javaTrain; public class Train14_1 {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0) return head; //特殊情况
ListNode pNode = head;
int len = 1;
while(pNode.next != null){
pNode = pNode.next;
len++;
}
k = len-k%len;
pNode.next = head; //注意此时pNode是原来的尾节点
for(int i = 0;i < k;i++){
pNode = pNode.next;
}
head = pNode.next;
pNode.next = null;
return head;
}
}
</span>
【LeetCode】 Rotate List 循环链表的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode——Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- [leetcode]Rotate List @ Python
原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...
随机推荐
- 最新Mac安装CocoaPods详细教程及各种坑解决办法
网上有很多教程,但要么内容很老,要么不详细,要么各种坑的情况没写.最近买新电脑了,正好要走一遍这些流程,所以写下次教程. 一.安装RVM及更新Ruby 安装RVM的目的是为了更新Ruby,如果你的Ru ...
- 【SQL】查询数据库中某个字段有重复值出现的信息
select name,mobile from [GeneShop].[dbo].[xx_member] where mobile in ( SELECT mobile FROM [GeneShop] ...
- 用C++实现Huffman文件编码和解码(2 总结)
这个是代码是昨天写完的,一开始的时候还出了点小bug,这个bug在晚上去吃饭的路上想明白的,回来更改之后运行立刻完成最后一步,大获成功. 简单说下huffman编码和文件压缩主要的技术. Huffma ...
- 倍福TwinCAT(贝福Beckhoff)应用教程13.2 TwinCAT控制松下伺服 CS说明
虚拟仿真上,要注意仿真只是为了可视化,可以看到数据的变动是否和实际一致,所以Robot2D才是主要因素,虚拟仿真采集机器人的关节位置或者TCP位置来显示而已,为了测试一些别的算法,我们还可以在虚拟仿真 ...
- CSDN个人空间、问答频道停站维护公告
各位亲爱的用户: CSDN个人空间.问答频道将于2014年11月17日18点至11月17日24点进行系统维护升级,升级期间会影响大家的正常訪问和操作.给大家带来不便.敬请广大用户谅解 ...
- Linux学习笔记 (四)归档和压缩
一.zip压缩命令: 1.压缩文件: 格式:zip 压缩文件 源文件 例:zip abc.zip abc //将abc文件压缩到abc.zip文件内. 2.压缩目录: 格式:zip –r 压缩目录 ...
- Win10 系统变成黑白屏幕了怎么办?
快捷键"windows徽标键+Ctrl+C"可以切换屏幕黑白了 这是win自带的一个"应用颜色筛选器", 桌面右键,选择个性化 点到背景页面,下拉,点击&q ...
- formidable 模块化开发 代码拆分(解耦) nodejs图片服务器架构
引言:程序要做到:健壮性.低耦合.可扩展.方便程序员分工合作 上传图片值nodejs服务器并显示图片的源代码: post.html : <!DOCTYPE html> <html l ...
- jQuery 获取DOM节点的两种方式
jQuery中包裹后的DOM对象实际上是一个数组,要获得纯粹的DOM对象可以有两种方式: 1.使用数组索引方式访问,例如: var dom = $(dom)[0]; 如: $("#id&qu ...
- C语言文件操作函数大全(超详细)
C语言文件操作函数大全(超详细) 作者: 字体:[增加 减小] 类型:转载 本篇文章是对C语言中的文件操作函数进行了详细的总结分析,需要的朋友参考下 fopen(打开文件)相关函数 open,fc ...