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 ...
随机推荐
- 客户端socket调用
import java.net.Socket; import java.io.*; import java.util.Scanner; import java.util.regex.Pattern; ...
- XAMPP 的MYSQL无法启动
最近在学习小程序,小程序的后台cms系统需要安装XAMPP软件,这个软件是个集成软件,里面有MySql 和 Apache 等 ,首先进入小程序后台时需要启动MySql 和 Apache Apache ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- Json-lib用法
Json-lib用法 1.需要的jar包有一下几个,别的文章中没有xom这个jar包,但我的工程中如果没有这个包,那么,json数据转换为xml数据的程序中在执行XMLSerializer xmlSe ...
- JS 阻止整个网页的内容被选中
pretty-girl { -webkit-user-select: none; } 可是!可是!不是每个浏览器都可以不忧桑!!!那就只能请脚本大王出山了. 阻止选中 有时候,我们需要禁止用户选中一些 ...
- Centos6.3手动rpm安装gcc,c++
如果你的服务器是不能上网的,那就说明你要手动安装很多软件,比如gcc; 1,首先到http://vault.centos.org/6.3/os/x86_64/Packages/下载用到的rpm包,包括 ...
- imagick-3.1.0RC2 安装错误
PHP 5.4.8 安装 imagick-3.1.0RC2 安装冒出一大堆错误, 貌似跟 ImageMagick-6.8.0-2 这个版本有关系, 我之前换成低版本的 ImageMagick 就可以顺 ...
- js 对象 视频 插入元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Sass与Compress实战:第一章
1.消除冗余代码的方式: ▶通过变量来复用属性值 例如,一段冗余的CSS代码: h1#brand { color : #1875e7 } #sidebar { background-color : # ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset
题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...