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 ...
随机推荐
- Python Day 12
阅读目录: 内容回顾 函数默认值的细节 三元表达式 列表与字典推导式 函数对象 名称空间 函数嵌套的定义 作用域 ##内容回顾 # 字符串的比较 -- 按照从左往右比较每一个字符,通过字符对应的asc ...
- https多网站1个IP多个SSL证书的Apache设置办法
这些天接触了解SSL证书后,写了一篇<申请免费的SSL证书,开通https网站>博文,其中简单记录了Apache的设置,后来又涉及到多个域名.泛域名解析.通配符SSL证书.单服务器/多服务 ...
- dremio jdbc使用
驱动包地址 链接:https://pan.baidu.com/s/1Nivkvze24hRH8pXOQleCgw 提取码:gp9z 使用dremio主要原因 : 1)springboot提供了es组件 ...
- JavaScript -DOM 编程艺术 2nd 完
今日看完了这本书,做完了最后一个综合性例子.说实话收获良多,终于明白前端-h5 具体做什么 越学习越无知,这个看来真是一个真理. 后期计划: 1.CSS + DIV 布局深入了解,重点实战 2.Jav ...
- mysql实现多实例
> mariadb安装 yum install mariadb-server > 创建相关目录,及设置权限 mkdir /mysqldb; mkdir /mysqldb/{33 ...
- android-音量管理
推荐简书夕月风: 前两个主要设计音频建设置流程. 一.https://www.jianshu.com/p/a48fc2c830da 二.https://www.jianshu.com/p/892761 ...
- 【ProtoBuffer】windows上安装ProtoBuffer3.1.0 (附已编译资源)
------- 17.9.17更新 --- 以下这些方法都是扯淡,对我的机器不适用,我后来花了最后成功安装并亲测可用的方法不是靠vs编过的,vs生成的库引入后函数全部报undefine refere ...
- web专业课学习及往后方向发展
日常10点起床!!!! web主要是网页设计,目前自我方向是学习web前端开发,熟悉掌握相关的编辑应用已达到能设计出满意的网页,日后继续学习后端等 ,成为全栈工程师.
- python+unittest 控制用例的执行顺序
unittest的main()方法执行用例的顺序是按照测试类.测试方法的名字的ASCII顺序来执行测试方法.所以可能执行的顺序和你想要的顺序不一样,可能通过下面两种方法修改执行顺序 1. 通过Test ...
- Maven4-仓库
坐标和构建是一个构件在Maven世界中的逻辑表示方式,而其物理表示方式是文件.Maven通过仓库来统一管理这些文件 什么是Maven仓库? 在Maven世界中,任何一个依赖,插件或者项目构建的输出,都 ...