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 ...
随机推荐
- 别人的Linux私房菜(15)磁盘配额与高级文件系统管理
磁盘配额在网站.邮件.文件等服务器常见,主要有针对用户.用户组.限制某一目录的的最大磁盘配额. ext文件系统进能针对整个文件系统配额,xfs可以针对目录配额.配额和文件系统有关. 内核必须支持磁盘配 ...
- T-4-java核心API-集合类
一.集合 用于存储类型一致的一组对象的数据结构. 类似于数组,但是集合提供了操作算法:集合=数据存储+操作算法.集合的用途极其广泛,如歌曲列表,联系人列表对话记录等. 集合比数组多了操作算法,便于提高 ...
- 《Linux就该这么学》第十八天课程
1.使用MariaDB数据库管理系统 今天没什么笔记,就不发了.想深入学习的可以前往原创地址:https://www.linuxprobe.com/chapter-18.html 图18-1 Mari ...
- 2019.03.28 bzoj3325: [Scoi2013]密码(manacher+模拟)
传送门 题意: 现在有一个nnn个小写字母组成的字符串sss. 然后给你nnn个数aia_iai,aia_iai表示以sis_isi为中心的最长回文串串长. 再给你n−1n-1n−1个数bib_ ...
- 使用vmware vconverter从物理机迁移系统到虚拟机P2V(多图)
zhuan:https://segmentfault.com/a/1190000002697929 本文完整记录了如何从物理服务器,保持所有环境配置信息,纹丝不动的迁移到虚拟机上,俗称 P2V .采用 ...
- linux pxe 安装Centos7
服务端 需要3种服务 dhcp + tftp + vsftp tftp 提供引导 为什么不使用其他协议来进行pxe引导 是因为网卡只会集成tftp这种服务 写明到镜像的方式 dhcp 下发tf ...
- winform中TextBox只能输入字母
private void txtTestPerson_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 'a' & ...
- Python之路【第三篇】编码
Python代码——>字节码——>机器码——>计算机 Windows: cmd ==> python 文件路径 cmd ==>python >> 输入命令 L ...
- Kerberos协议
Kerberos协议主要用于计算机网络的身份鉴别 (authentication),其特点是用户只需输入一次身份验证信息就可以凭借此验证获得票据(ticket-granting-ticket)访问多个 ...
- 包建强的培训课程(8):iOS与设计模式
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...