`
hcx2013
  • 浏览: 83896 次
社区版块
存档分类
最新评论

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.

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || k == 0) {
        	return head;
        }
        ListNode p = head;
        int len = 1;
        while (p.next != null) {
        	len++;
        	p = p.next;
        }
        p.next = head;
        k = k % len;
        for (int i = 0; i < len - k; i++) {
			p = p.next;
		}
        head = p.next;
        p.next = null;
        return head;
    }
}

 

 

9
9
分享到:
评论

相关推荐

    cpp-算法精粹

    Rotate List Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked ...

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    javalruleetcode-LeetCode:LeetCode算法问题

    RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 ...

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    Rotate List 最大不同 318. Maximum Product of Word Lengths 【这个题目LTE 复杂度已经降下了】 最长不重复字符串3. Longest Substring Without Repeating Characters ----2016.10.08 移动0到末尾 283. Move Zeroes...

    Android样式的开发:layer-list实例详解

    本文主要介绍Android样式开发layer-list,这里整理了详细的资料,及简单示例代码有兴趣的小伙伴可以参考下

    svg-classlist-polyfill:SVG元素上的Polyfills classList方法(包含,添加,删除和切换)

    SVG classList Polyfill IE11和其他旧版浏览器不支持的SVG元素上的classList方法(包含,添加,删除,切换)的Polyfill。... toggle ( 'icon--rotate' ) icon . classList . remove ( 'icon--blue' ) icon . classList

    bg-rotate.js:简单的背景图像滑块。 非常适合用于英雄或网站上您想要一些图像定期旋转和更改的任何地方

    images: ['/images/myimage.jpg', '/images/anotherimg.png', '/images/heroimgs/thirdimg.jpg'], //list of images you want in the rotating mix interval: 10000 //interval in milliseconds });

    python代码示例圣诞树和转盘抽奖

    python代码示例圣诞树和转盘抽奖 使用turtle(海龟)模块绘制圣诞树 和 转盘抽奖 代码实例 #制定点位置 ...rotateNumber = randint(80,100) #随机产生旋转次数80-100之间 angle = 25 #定义每次旋转25度

    大数据可视化(四)比例数据可视化.pdf

    opts=opts.AxisOpts( axislabel_opts=opts.LabelOpts(rotate=30,font_size=9)),#设置⼤⼩与倾斜度 ) .set_series_opts(label_opts=opts.LabelOpts(is_show=False)) .render("result/4-11.html") ) 矩形树图 矩形树图...

    cad 二次开发教程,很详细,配合cad使用

    PT_TMP1 (LIST (CAR PT_START) (CADR PT_END)) PT_TMP2 (LIST (CAR PT_END) (CADR PT_START))) (COMMAND "PLINE" PT_START PT_TMP1 PT_END PT_TMP2 "C") (COMMAND "SELECT" "L" "") ...

    编程珠玑全部源代码 分享

    rotate.c -- Three ways to rotate the elements of a vector. The next two program are used in a pipeline to compute all anagrams in a dictionary sign.c -- Sign each word by its letters in sorted order...

    PDF_To_JPG[无水印]

    -t [rotate] Rotate the tiff image,1-90,2-180,3-270 -l [last Page] Last page to convert -q [quality] jpeg quality 25-100 -? Help For Example: PDFtoJPG -i C:\abc.pdf -o C:\abc\ -r 300 -q 100 ...

    wowmodelview 含源代码

    WOW模型查看器,有源代码. WoWmodelview - World of Warcraft 3D model viewer This program displays 3D models from World of Warcraft: characters, creatures, spell ...- Equipment: choose from the popup list

    Shy-3D立体动态相册实现代码(源代码)

    list-style: none; } .box{ width:200px; height:200px; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-size:100% 100%; position: absolute; ...

    Python3实现的旋转矩阵图像算法示例

    本文实例讲述了Python3实现的旋转矩阵图像算法。分享给大家供大家参考,具体如下: ... def rotate(self, matrix): :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix i

    lrucacheleetcode-LeetCode:LeetCode刷题

    lru cache leetcode LeetCode 剑指offer LeetCode解题记录(python) 2018.9.19 两数之和(Two ...旋转数组的最小数字(Rotate The Smallest Number of Arrays) 2018.10.9 二叉搜索树的后序遍历序列

    扩展矩阵leetcode-leetcode:Leetnode笔记

    的个数),extend,extendleft,index,insert,pop,popleft,remove,reverse,rotate 面试表达 第1部分: 投射2D图像/向量/矩阵成一维数组/载体和使用二进制搜索找到的边界 假设我们有一个 10x11 的图像,如图1所...

    STL 源码剖析(侯捷先生译著)

    源码之前了无秘密,你将看到vector的实现、list的实现、heap的实现、deque的实现、RB-tree的实现、hash-table的实现、set/map 的实现;你将看到各种算法(排序、搜寻、排列组合、数据移动与复制…)的实现;你甚至将...

    STL源码剖析.pdg

    4.3.6 list 的元素操作:push_front, push_back, erase, pop_front, 136 pop_back, clear, remove, unique, splice, merge, reverse, sort 4.4 deque 143 4.4.1 deque 概述 143 4.4.2 deque 的中控器 144 4.4.3...

    Android代码-CursorWheelLayout

    It behaves like a Circular ListView where items rotate rather than scroll vertically(but without view recycle strategy). CursorWheelLayout consists of two components , the center item with id id_...

Global site tag (gtag.js) - Google Analytics