Rotate List leetcode java
题目:
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个node,从那开始到结尾和之前那部分对调,那个例子就是,4->5拿前面来,1->2->3拿后面去。
几个特殊:
k是可以大于整个list的长度的,所以这时要对k对len取模
如果取模之后得0,相当于不用rotate,直接返回
处理完特殊情况后,就用熟悉的faster/slower双指针解决就好(看到这种linkedlist,倒着数数的,就条件反射了)
先对faster设步长为n,然后faster和slower再一起走,知道faster.next==null,说明slower指向要倒着数的开始点的前一个位置。
所以slow.next就是要返回的newhead,保存一下。
然后把faster.next接到head上,slower.next存为null,作队尾。
这样就把list给rotate了。
这是我想的一种解法,还有一种就是把整个list连起来,变成环,找到切分点断开就行。
解法1:
1 public ListNode rotateRight(ListNode head, int n) {
2 if(head==null||head.next==null||n==0)
3 return head;
4 ListNode fast = head, slow = head,countlen = head;
5 ListNode newhead = new ListNode(-1);
6 int len = 0;
7
8 while(countlen!=null){
9 len++;
countlen = countlen.next;
}
n = n%len;
if(n==0)
return head;
for(int i = 0; i < n; i++)
fast = fast.next;
while(fast.next!=null){
slow = slow.next;
fast = fast.next;
}
newhead = slow.next;
fast.next = head;
slow.next = null;
return newhead;
}
解法2:
1 public ListNode rotateRight(ListNode head, int n) {
2
3 if (head == null || n == 0)
4 return head;
5 ListNode p = head;
6 int len = 1;//since p is already point to head
7 while (p.next != null) {
8 len++;
9 p = p.next;
}
p.next = head; //form a loop
n = n % len;
for (int i = 0; i < len - n; i++) {
p = p.next;
} //now p points to the prev of the new head
head = p.next;
p.next = null;
return head;
}
Reference for 2: http://leetcodenotes.wordpress.com/2013/08/14/leetcode-rotate-list-%E6%8A%8A%E5%90%8Ek%E4%B8%AArotate%E5%88%B0list%E5%89%8D%E9%9D%A2%E5%8E%BB%EF%BC%8Ck%E5%8F%AF%E4%BB%A5%E8%B6%85%E8%BF%87list%E6%9C%AC%E8%BA%AB%E9%95%BF%E5%BA%A6/
Rotate List leetcode java的更多相关文章
- Rotate Image leetcode java
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
- LeetCode算法题-Rotate Array(Java实现)
这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- 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 ...
- 48. Rotate Image - LeetCode
Question 48. Rotate Image Solution 把这个二维数组(矩阵)看成一个一个环,循环每个环,循环每条边,每个边上的点进行旋转 public void rotate(int[ ...
随机推荐
- Java中面向对象的理解
按照惯例,先做一个简单的介绍,现在开始学习 Thinging in Java 4 ,一边看,一边记录,我都不想给自己设定时间安排了,毕竟很少实现过.所以就这样吧!不定期的更新,我都会放到博客中的. 所 ...
- [leetcode greedy]134. Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Swift2.0语言教程之类的方法
Swift2.0语言教程之类的方法 Swift2.0语言的方法 方法其实就是函数,只不过它被定义在了类中.在Swift中,根据被使用的方式不同,方法分为了实例方法和类型方法两种.这两种方法的定义也和O ...
- 在Windows上安装FFmpeg程序
原文地址:http://helloway.blog.51cto.com/7666282/1642247 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它提供了录 ...
- QT学习笔记3:QT中语法说明
一.Qt 类中Q_OBJECT的作用 QObject 是所有Qt对象的基类. QObject 是Qt模块的核心.它的最主要特征是关于对象间无缝通信的机制:信号与槽.使用connect()建立信号到槽的 ...
- ZJOI2018 day2游记
省选讲课:还不错吧 ZJOI RP++ Day2: 题出的好!覆盖知识点广,题目又着切合实际的背景,解法比较自然. 给出题人点赞 ! 意识模糊地点开了题(考前不熬夜似乎还是很困qaq) T1:前一个小 ...
- 移动web开发经验总结(1)
1.<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, minimum-sca ...
- Python学习笔记(八)—集合的学习
集合(set)是一个无序不重复元素的序列. 基本功能是进行成员关系测试和删除重复元素. 1.集合的作用 它可以把一个列表中重复的数据去掉,而不需要你再写判断 可以做关系测试,比如说有两个科目,一个数学 ...
- JDK篇
卸载系统自带的jdk 使用以下命令查看是否已经安装了jdk rpm -qa|grep java rpm -qa|grep jdk 如果已经安装了可能会得到下面的结果: java-1.4.2-gcj ...
- 不同的activity使用bundle对象传值给广播接收器
解决了一下午的问题,广播机制传值,在一个activity中发送广播给广播接收器,使用的是同一个action 在另一个activity中如果也发送广播给同一个广播接收器,使用相同的action,会导致后 ...