LeetCode OJ 61. Rotate List
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%list.length。
解决这个问题我分为了两个步骤:
- 求链表的长度;
- 进行旋转操作;
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(k<0 || head==null) return null; int length = 0;
ListNode temp = head;
ListNode temp2 = head;
while(temp!=null){
temp = temp.next;
length++;
}
k = k%length;
temp = head; while(temp.next != null){
if(k<1) temp2 = temp2.next;
temp = temp.next;
k--;
}
temp.next = head;
head = temp2.next;
temp2.next = null;
return head;
}
}
参考了别人的代码,找到了更加简便的方法,这个方法只用到了一个指针,很值得我们学习。
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0) {
return head;
}
ListNode p = head;
int len = 1;
while(p.next != null) {
p = p.next;
len++;
}
p.next = head;
k %= len;
for(int i = 0; i < len - k; i++) {
p = p.next;
}
head = p.next;
p.next = null;
return head;
}
LeetCode OJ 61. Rotate List的更多相关文章
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【一天一道LeetCode】#61. Rotate List
一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...
- LeetCode OJ 48. Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode OJ 189. 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】61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- LeetCode OJ:Rotate List(旋转链表)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- LeetCode OJ:Rotate Image(旋转图片)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode OJ: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 OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
随机推荐
- Linux常用命令汇总及使用方法(二)之文本编辑器VI
VI可能是在Linux中使用比较频繁的文本编辑器,如果不能熟练使用VI,在一定程度上会影响工作效率,所以在这里记录一下VI的常用命令及操作方式 在[root@test ~]# vi carrie.tx ...
- hibernate java.sql.SQLException
异常:java.sql.SQLException oracle.net.ns.NetException java.net.ConnectException 提示:The Network Adapter ...
- Scope Directive
---------------------------Scope-------------------------------- https://docs.angularjs.org/guide/sc ...
- Angularjs directive全面解读(1.4.5)
说到Angularjs directive即指令,可以这么说Angularjs的灵魂就是指令,学会Angularjs指令那么你的Angularjs的武功就修炼了一半了,当然这只是鄙人的一点点独到见解, ...
- Hibernate5--课程笔记1
Hibernate简介: Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hib ...
- Oracle数据库初探
一.安装oracle数据库 步骤:转载一个很不错的文档:http://www.linuxidc.com/Linux/2015-02/113222.htm 注意点:安装的时候会check相关依赖,有些可 ...
- 浏览器 CSS 兼容写法的测试总结
做前端最讨厌的就是 IE6,7,8,虽然被淘汰的浏览器,但是在中国用户仍然很多,不可能像国外网站一样直接就不管它了,这样会流失很多流量啊. 现在有了IE9,IE10还好些,几乎和 Chrome,Fir ...
- ggplot2 坐标系相关设置(coord)
在ggplot中,未来更好的数据可视化效果,我们有时候可能要用到一些坐标转换的操作,比如要画横向条形图或者蜘蛛图等. coord_cartesian(xlim = NULL, ylim = NULL) ...
- txt文件的读取
两个函数:textread或importdata [textread函数] 格式:I=textread('文件名.txt','列',读取的行数,'headerlines',跳过的头行数); 返回值I ...
- Linux的网卡由eth0变成了eth1,如何修复
Linux的网卡由eth0变成了eth1,如何修复 使用wmware安装了linux,安装成功后,使用的网卡是eth0,没有eth1.但是用过一段时间后,不知道为什么eth0无法使用,系统却自动生 ...