leetcode — rotate-list
/**
* Source : https://oj.leetcode.com/problems/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.
*
*/
public class RotateList {
/**
* 以链表中某一个元素为支点翻转链表
* 如果用Java中的链表做,反而不容易,可以构造一个单向链表
* 找到链表的尾部
* 将链表的尾部指向头部,也就是构成一个环形链表,并记录链表总数n
* 旋转之后支点前面的长度为 n-k,则从链表尾部移动 n-k个节点,该位置的next为新的head,将该位置的next置为空也就是打断环形链表
*
*
* 边界条件
* 链表为空或者k <= 0直接返回head
* 如果k >= n 实际上 k = k % n
*
* @param head
* @param k
*/
public Node rotate(Node head, int k) {
if (k <= 0 || head == null) {
return head;
}
Node p = head;
int n = 1;
// 获取链表总数、tail
while (p.next != null) {
p = p.next;
++n;
}
// 链表tail
p.next = head;
k = n - k % n;
for (int i = 0; i < k; i++) {
p = p.next;
}
head = p.next;
p.next = null;
return head;
}
private static class Node implements Comparable<Node>{
int value;
Node next;
@Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
}
@Override
public int compareTo(Node o) {
return this.value - o.value;
}
}
private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
System.out.println();
}
public static Node generateList (int n) {
Node list = new Node();
list.value = 1;
Node pointer = list;
for (int i = 2; i <= n; i++) {
Node node = new Node();
node.value = i;
pointer.next = node;
pointer = pointer.next;
}
return list;
}
public static void main(String[] args) {
RotateList rotateList = new RotateList();
print(rotateList.rotate(generateList(5), 5));
print(rotateList.rotate(generateList(5), 2));
print(rotateList.rotate(generateList(5), 0));
print(rotateList.rotate(generateList(5), 4));
print(rotateList.rotate(null, 2));
}
}
leetcode — rotate-list的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [LeetCode] 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] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode——Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- [leetcode]Rotate List @ Python
原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...
- [LeetCode] Rotate Function 旋转函数
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
随机推荐
- latex 希腊字母表示
http://blog.sina.com.cn/s/blog_5e16f1770100lxq5.html
- <笔记>TP5的save方法返回值
用save方法来更新数据时,若更新前后数据没有改变则返回0,更新成功返回影响行数,更新失败返回false 若想要数据没改变时提示修改成功,则需要严格判断 if(结果!==false){提示成功}而不是 ...
- gitlab 之 升级、迁移
-----故事背景- 公司服务器用vm装的虚拟机,由于公司服务器经常无故重启,且找不到原因,所以公司准备将vm迁移至Hyper-V,Hyper-V可以自启动虚拟机且免费. -----升级.迁移- 首先 ...
- jdk1.8.0环境变量设置
jdk1.8.0环境变量设置 1.jdk安装完毕 打开如下链接:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloa ...
- 自动的输入号码的辅助软件在ie浏览器下的尝试
在ie下面的自动输入号码的软件,我已经折腾了好久.由于ie环境非常复杂:网页延迟这个时间一直不能准确的得到,这个时间主要包括:网络的与服务器的交换速度,网页自身的加载速度,网页的js渲染的效果的影响. ...
- NGUI 摇奖滚轮
效果图: 优缺点: 优点: 1.一条曲线完美解决旋转问题 2. 解决了超速的问题,现在速度再快也不会乱了 3.快速停止的时候进行了进度区分,后面的会等前面的停了再停 缺点: 1.停止节奏上会有细微差距 ...
- Android-Could not download kotlin-reflect.jar
在AndroidStudio中报以下错误: 错误详情: Could not download kotlin-reflect.jar (org.jetbrains.kotlin:kotlin-refle ...
- 【Java】利用注解和反射实现一个"低配版"的依赖注入
在Spring中,我们可以通过 @Autowired注解的方式为一个方法中注入参数,那么这种方法背后到底发生了什么呢,这篇文章将讲述如何用Java的注解和反射实现一个“低配版”的依赖注入. 下面是我们 ...
- Java并发编程基础——同步
一.synchronized 关键字 1)synchronized 锁什么?锁对象.可能锁对象包括: this, 临界资源对象,Class 类对象.如同下面例子所示: package cn.test. ...
- 原来你离BAT只有一步之遥
ladies and乡亲们 喜迎全民嗨购双11 i春秋准备搞一波大优惠 优惠力度有多大 跨店凑单满400-50? 指定商品199减100? 史无钜惠 不凑单 不指定 一次直降9000元 原价:2580 ...