【LeetCode】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
.
start with an example.
Given [0,1,2]
, rotate 1 steps to the right -> [2,0,1]
.
Given [0,1,2]
, rotate 2 steps to the right -> [1,2,0]
.
Given [0,1,2]
, rotate 3 steps to the right -> [0,1,2]
.
Given [0,1,2]
, rotate 4 steps to the right -> [2,0,1]
.
So, no matter how big K, the number of steps is, the result is always the same as rotating K % n
steps to the right.
public class Solution {
public ListNode rotateRight(ListNode head, int n) {
if(head==null||head.next==null||n==0)
return head;
int len = 0;
ListNode root = head;
while(root!=null){
root=root.next;
len++;
} ListNode fast = head;
ListNode slow = head;
int i=n%len;
if(i==0)
return head;
while(i>0&&fast!=null){
fast=fast.next;
i--;
} while(fast.next!=null){
slow=slow.next;
fast=fast.next;
} ListNode tem = slow.next;
fast.next=head;
slow.next=null;
return tem; }
}
【LeetCode】Rotate List的更多相关文章
- 【LeetCode】 Rotate List 循环链表
题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...
- 【leetcode】Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 【题解】【矩阵】【回溯】【Leetcode】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
随机推荐
- Python学习笔记2_一些小程序
counts = [98,12,3,4,1,4,9,3821] minNum = min(counts) #print minNum minNum_index = counts.index(minNu ...
- AC日记——Dishonest Sellers Codeforces 779c
C. Dishonest Sellers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- codevs——2956 排队问题
2956 排队问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有N个学生去食堂,可教官规定:必须2人或3 ...
- [ONTAK2010]Peaks
题目大意: 一个图上有$n(n\leq100000)$个带权点,$m(m\leq500000)$条带权边.有$q(q\leq500000)$组询问,每次询问从点$v$出发,只经过权值小于等于$x$的边 ...
- jvm 问题分析
jmap dump:file=[文件名].dump [进程号] 生成dump root@VM-185-251-ubuntu:/opt/scripts# jmap -dump:file=three.d ...
- openlayer3 加载geoserver发布的WFS服务
转自原文 openlayer3加载geoserver发布的WFS服务 openlayers3调用GeoServer发布的wfs 1 参考一 1.1 问题 openlayer3加载WFS存在跨域问题,需 ...
- Android 自定义ListView Item侧滑删除
本程序是基于网上开源项目修改而来,具体来源忘了,懒得搜了,如果有不合适的地方,请原作者联系我,我会及时回复和处理的! 该例子程序中主要包含两个ListView,一个是实现侧滑删除,一个是侧滑出菜单,代 ...
- 高通msm8994启动流程简单介绍
处理器信息 8994包括例如以下子系统: 子系统 处理器 含义 APSS 4*Cortex-A53 应用子系统 APSS 4*Cortex-A57 应用子系统 LPASS QDSP6 v5.5A(He ...
- Delphi TScrollBar 用于滚动窗口、组件内容
滚动条组件(TScrollBar)此组件是一个Windows滚动条,用于滚动窗口.组件内容.许多控制有滚动条属性,它们把滚动条作为自己的一部分,对于没有完整滚动条的控制,TScrollBar组件提供了 ...
- python(35)- 异常处理
一 错误和异常 part1:程序中难免出现错误,而错误分成两种 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 ...