/**
* 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的更多相关文章

  1. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  2. [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  ...

  3. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  4. [LeetCode] Rotate Image 旋转图像

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  5. LeetCode——Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given  ...

  6. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  7. [leetcode]Rotate List @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...

  8. [leetcode]Rotate Image @ Python

    原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...

  9. 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 = ...

  10. [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 ...

随机推荐

  1. 20175316 盛茂淞 Arrays和String单元测试

    Arrays和String单元测试 具体描述: 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Arr ...

  2. JAVA 8 主要新特性 ----------------(七)新时间日期 API -----LocalDate

    一.改版原因 1.老板的Date和Calander存在问题,日期操作名称混乱,有的在text下,有的在util下,包名混乱         2.Simple包混乱,致命错误线程不安全.        ...

  3. AlphaGo原理浅析

    一.PolicyNetwork(走棋网络) 首先来讲一下走棋网络.我们都知道,围棋的棋盘上有19条纵横交错的线总共构成361个交叉点,状态空间非常大,是不可能用暴力穷举的方式来模拟下棋的.但是我们可以 ...

  4. spring 5.1.2 mvc RequestMappingHandlerMapping 源码初始化过程

    RequestMappingHandlerMapping getMappingForMethod RequestMappingHandlerMapping 继承于 AbstractHandlerMet ...

  5. Maven3-依赖

    依赖配置 我们先来看一份简单的依赖声明: <project> ... <dependencies> <dependency> <groupId>...& ...

  6. 04、SQL 查询当天,本月,本周的记录

    SELECT * FROM 表 WHERE CONVERT(Nvarchar, dateandtime, 111) = CONVERT(Nvarchar, GETDATE(), 111)   ORDE ...

  7. CSS3中的一些属性

    1. 可匹配部分字符串 2. box-sizing属性 3. CSS3多栏布局 1.可匹配部分字符串 /*^运算符,匹配字符串首部*/ a[href^='http://website'] /*$运算符 ...

  8. JavaScript工作体系中不可或缺的函数

    一.函数的概念 日常生活中,我们要完成一件事,总是习惯先有一个计划,后期按照计划,一步一步执行,则能够完成,并且达到一定效果实现一定的功能.在编程的世界里,“功能”可称呼为“函数”,因此“函数”即一段 ...

  9. wordpress升级版本时出现错误“Maximum execution time of 30 seconds exceeded”

    wordpress版本是4.9,之前升级5.0时就提示这个错误了,但因为我用的第三方主题,所以也没想去解决,也担心升级了wp版本后主题出问题. 现在wp版本已经到了5.2了,我闲着无聊就又点了升级,结 ...

  10. Javascript高级编程学习笔记(50)—— DOM2和DOM3(2)样式

    样式 在HTML中定义元素的方式有以下三种: 1.link标签引入外部样式表 2.style标签定义嵌入样式 3.通过JS中对style特性定义元素样式(行内样式) “DOM2级样式”围绕上述样式机制 ...