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.

原题链接:https://oj.leetcode.com/problems/rotate-list/

得到链表长度。链表指向尾节点,将链表首尾相连,向右k%len。得到结果链表的尾节点。

public class RotateList {
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode head1 = new ListNode(2);
ListNode head2 = new ListNode(3);
ListNode head3 = new ListNode(4);
ListNode head4 = new ListNode(5);
head.next = head1;
head1.next = head2;
head2.next = head3;
head3.next = head4;
// while(head != null){
// System.out.println(head.val);
// head = head.next;
// }
ListNode ln = new RotateList().rotateRight(head, 2);
while(ln != null){
System.out.println(ln.val);
ln = ln.next;
}
}
public ListNode rotateRight(ListNode head, int n) {
if(head == null)
return head;
int len = 1;
ListNode tmp = head;
while(tmp.next != null){
tmp = tmp.next;
len ++;
}
tmp.next = head;
n %= len;
int step = len - n;
while(step > 0){
tmp = tmp.next;
step --;
}
head = tmp.next;
tmp.next = null;
return head;
} }
// Definition for singly-linked list.
class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
next = null;
}
}

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 Image(矩阵旋转)

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

  6. [leetcode]Rotate List @ Python

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

  7. [leetcode]Rotate Image @ Python

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

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

  9. [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. Linux下编译Qt源码,一定要下载tar.gz版本,否则会报权限不足

    首先下载qt-everywhere-opensource-src-4.8.1源码,下载地址: ftp://ftp.qt-project.org/qt/source/ 在Linux下编译一定要下载qt- ...

  2. delphi与汇编

    我一直认为Delphi功能与C++相比毫不逊色,提供了丰富的控件和类.全部API以及嵌入的汇编.最近小弟在把C版的Huffman压缩改用Delphi写时,顺便“研究”了一下Delphi的位操作和嵌入式 ...

  3. Magento给产品添加“new”或者折扣数量标签 magento new label. discount label

    文章最底部有效果图. 给新产品添加“new”的标签.给折扣产品,显示出折扣的数量. 这个可以自己写一段代码加在到模板文件夹下面的catalog/product/list.phtml中. 以下是代码 & ...

  4. 初识Dubbo 系列之4-Dubbo 依赖

    依赖 必需依赖 JDK1.5+ 理论上Dubbo能够仅仅依赖JDK,不依赖于不论什么三方库执行,仅仅需配置使用JDK相关实现策略. 缺省依赖 通过mvn dependency:tree > de ...

  5. winform基础——数据访问及几个案例

    数据访问分为三个部分:(1)创建链接(2)创建与执行命令(3)读取或准备相关数据 一,需要引用的命名空间 using data: using data.SqlClient; 二,创建与数据库的链接—— ...

  6. Mac 安装工具包brew

    linux有命令行工具 apt-get ,Mac 下也有类似的brew 也就是HomeBrew 网址:http://brew.sh/index_zh-cn.html 可以看到mac安装的时候只需要执行 ...

  7. Java使用HttpURLConnection上传文件

    从普通Web页面上传文件非常easy.仅仅须要在form标签叫上enctype="multipart/form-data"就可以,剩余工作便都交给浏览器去完毕数据收集并发送Http ...

  8. 2388 Who's in the Middle(简单排序)

    训练计划的第一个问题,首先从水问题开始:排序的数组,中间数则输出. http://poj.org/problem?id=2388 冒泡排序: #include <iostream> usi ...

  9. QT5.6,5.7,5.8的新特征以及展望

    https://wiki.qt.io/New_Features_in_Qt_5.6 (跨平台High-DPI,改进WebEngine到45,支持WIN 10,Canvas3D,3D) https:// ...

  10. HDU/HDOJ 2612 Find a way 双向BFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路:从两个起点出发,有多个终点,求从两个起点同时能到达的终点具有的最小时间,开两个数组分别保存 ...