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 ...
随机推荐
- 将图片设置给ImageView时的属性配置
将图片设置给ImageView的时候,由于图片大小和逻辑需求的不确定会造成实际产生的效果和我们实际的需求不符的情况,这时需要对imageVIew控件添加scaleType属性,下面我用两张图片帮大家轻 ...
- 我的linux云服务器配置记录
配置vps的时候,随手记录一下~~ 添加一些源: vi /etc/apt/sources.list deb http://mirrors.aliyun.com/ubuntu/ xenial main ...
- [妙味DOM]第三课:Event-事件详解1
知识点总结 焦点事件 onfocus 获取焦点 onblur 失点焦点 obj.focus() 给指定元素设置焦点 obj.blur() 取消指定元素的焦点 obj.select() 选择指定元素里的 ...
- struts2防止重复提交的标签
struts2 token 使用说明 --------------------------------------------------------------------------------- ...
- aws部署从无到有
第一次进入aws控制台的时候傻眼了,密密麻麻的菜单都不知道该选哪一个.无从下手 查了好久才找到,就从EC2入手吧,点击后EC2后进入下面页面 点击启动实例,这个页面就亲切多了.不在像看天书了. 本人最 ...
- CF 602B Approximating a Constant Range
(●'◡'●) #include<iostream> #include<cstdio> #include<cmath> #include<algorithm& ...
- queue STL
//queue STL //queue is just a container adaptor, which is a class that use other container. //just l ...
- spring源码
今天看了看spring对于视图解析的源码,发现还不是那些思想,internalResourceView里的一个渲染方法 protected void exposeModelAsRequestAttri ...
- 带宽 VS CDN (转载)
并发的影响因素:带宽.web server(含php).static server.数据库 带宽价格: 静态IP企业宽带 5M 10M 50M 100M 200M 盛大云 华东双线 216/月 396 ...
- 3.编写Java应用程序。首先定义一个描述银行账户的Account类,包括成员变 量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次, 编写一个主类,在主类中测试Account类的功能。
Account package com.hanqi.test; public class Account { private String zhanghao;private double yve; A ...