Rotate List —— LeetCode
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位。
解题思路:踩了个坑,k有可能比链表的长度还长,比如长度为3的链表,k=2000000000,所以开始需要遍历一下链表算出长度,k%=len,然后就是两个pointer一个先走k步,另一个再走,最后把末尾的next节点设为head,新head就是慢指针指向的节点。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null||k == 0){
return head;
}
int fast = k;
ListNode fastNode = head;
ListNode slowNode = head;
ListNode pre = slowNode;
int len = 0;
while(fastNode!=null){
len++;
fastNode=fastNode.next;
}
fast %= len;
fastNode = head;
if(fast == 0){
return head;
}
while(fastNode!=null&&fast-->0){
fastNode = fastNode.next;
}
while(fastNode!=null){
fastNode = fastNode.next;
pre = slowNode;
slowNode = slowNode.next;
}
ListNode newHead = new ListNode(0);
newHead.next = slowNode;
pre.next = null;
while(slowNode.next!=null){
slowNode = slowNode.next;
}
slowNode.next = head;
return newHead.next;
}
}
Rotate List —— LeetCode的更多相关文章
- 48. Rotate Image - LeetCode
Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...
- Rotate Image [LeetCode]
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- Rotate Array leetcode
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- Rotate Image leetcode java
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- Rotate List leetcode java
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- 189. Rotate Array - LeetCode
Question 189. Rotate Array Solution 题目大意:数组中最后一个元素移到第一个,称动k次 思路:用笨方法,再复制一个数组 Java实现: public void rot ...
- 796. Rotate String - LeetCode
Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...
- Rotate List || LeetCode
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * } ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
随机推荐
- Log4j(1.2.17) - hello world
1. Maven 依赖 <dependencies> <dependency> <groupId>log4j</groupId> <artifac ...
- java 钱币的单位转换
将钱转成转换为带指定单位的钱 int money = 10; NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); System ...
- WisDom.Net 框架设计(三) 数据缓存
WisDom.Net --数据缓存 1.几种缓存方式 1.静态全局变量 C#静态变量使用 static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明 ...
- "库未注册"(Library not registered)异常.
启发链接:http://social.msdn.microsoft.com/Forums/vstudio/en-US/f25b80bc-ecd4-4c37-8be3-9106a765b072/libr ...
- sql uniqueidentifier转varchar
--- DECLARE @myid uniqueidentifierSET @myid = NEWID()SELECT CONVERT(char(255), @myid) AS 'char';GO-- ...
- javascript中常用的DOM事件
//常用事件 onclick 点击事件 onmousedown 鼠标按下 onmousemove 鼠标移动 onmouseup 鼠标抬起 onmouseover 鼠标放上 onmouseout 鼠标放 ...
- WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path
之前在mac上调试hadoop程序(mac之前配置过hadoop环境)一直都是正常的.因为工作需要,需要在windows上先调试该程序,然后再转到linux下.程序运行的过程中,报Failed to ...
- CetnOS minimal 网络不可用
系统版本: CentOS-6.6-i386-minimal 问题说明: CentOS minimal 在安装完成之后,网络不可用,一些常见的命令报错,如: ping: unknow host xxxy ...
- AspNet WebApi : MessageHandler(消息处理器 )
1. Http Message Handler WebApi中的MessageHandler类似MVC中的filter,可用于请求/响应到达真正目标前对请求或者响应进行修改,比如:用户身份验证,请求头 ...
- Android 中文 API (29) —— CompoundButton
前言 本章内容是android.widget.CompoundButton,翻译来自德罗德,再次感谢德罗德 !期待你一起参与Android API 的中文翻译,联系我over140@gmail.com ...