第22题 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.
Solution:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int n) {
if(head==null||head.next==null || n==0) return head;
ListNode fakeHead = new ListNode(0);
fakeHead.next=head;
int length=0;
ListNode start=fakeHead, end=fakeHead;
for(int i=0; i<n; i++){
if(end.next==null) break;
end=end.next;
length++;
}
if(end.next==null){
//when n>=length
//int startPos = (length-n)%length+length; //Or:
int startPos=n%length;
if(startPos==0) return fakeHead.next;
startPos=length-startPos; for(int i=0; i<startPos; i++){
start=start.next;
}
}
else{
while(end.next!=null){
start=start.next;
end=end.next;
}
}
//reorder list
end.next=fakeHead.next;
fakeHead.next=start.next;
start.next=null;
return fakeHead.next; }
}
Note:假设end后移n次后end.next还不是null,说明list的长度>n。则将start和end总体后移。
否则,说明list的长度<=n,这时候须要找到rotate的实际次数。对于一个长为length的list,若rotate的次数为length,则结果为原list。所以rotate的实际次数是n%length,若结果为0,则直接返回原list;否则,须要找到对应的start点,即改动list的起始点。
注意代码中
int startPos = (length-n)%length+length;
事实上和
int startPos=n%length;
startPos=length-startPos;
一个效果(startPos不为0时)。可是后者比較好理解。
另外一个负数对一个正数取模,计算例如以下:
result=m%n, where m<=0, n>0
Assume m=xn+result, where x<=0, result<0 and -result<n.
比方(-5)%3。 -5=3*(-1)+(-2)。所以(-5)%3=-2。
第22题 Rotate List的更多相关文章
- 剑指offer 面试22题
面试22题: 题目:链表中倒数第k个节点 题:输入一个链表,输出该链表中倒数第k个结点. 解题思路:为了实现只遍历链表一次就能找到倒数第k个节点,我们可以定义两个指针.让第一个指针先向前走k-1步,第 ...
- c++作业22题
一.单选题(共22题,100.0分) 1 已知int i=5,下列do-while循环语句的循环次数是 do{ cout<<i - -<<endl; i - -; }while ...
- C语言 编程练习22题
一.题目 1.编一个程序,输入x的值,按下列公式计算并输出y值: 2.已知数A与B,由键盘输入AB的值,交换它们的值,并输出. 3.给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位 ...
- 记一道有意思的算法题Rotate Image(旋转图像)
题出自https://leetcode.com/problems/rotate-image/ 内容为: You are given an n x n 2D matrix representing an ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
- LeetCode算法题-Rotate Array(Java实现)
这是悦乐书的第184次更新,第186篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第43题(顺位题号是189).给定一个数组,将数组向右旋转k步,其中k为非负数.例如: ...
- 网络流 24 题汇总(LOJ 上只有 22 题???)
太裸的我就不放代码了...(黑体字序号的题表示值得注意) 1.搭配飞行员 [LOJ#6000] 二分图最大匹配. 2.太空飞行计划 [LOJ#6001] 最小割常规套路.输出方案.(注:这题换行符要用 ...
- 面试 16:栈的压入压出队列(剑指 Offer 第 22 题)
我们今天继续来看看周五留下的习题: 面试题:输入两个整数序列,第一个序列表示栈的压入顺序,请判断二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如:压入序列为{1,2,3,4,5},那{ ...
- OCP-1Z0-051-题目解析-第22题
22. You need to create a table for a banking application. One of the columns in the table has the fo ...
随机推荐
- SSH协议详解(转)
转发的http://blog.csdn.net/macrossdzh/article/details/5691924 很透彻啊,学习了 一.什么是SSH SSH是英文Secure Shell的简写形式 ...
- Jquery中toggleClass的两种用法
css样式: <style type="text/css"> .bgc{ background-color:#F00; color: #FFF} </style& ...
- Jquery 时间格式化
var TimeObjectUtil;/** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * ...
- meta标签详解:源http://blog.csdn.net/kongjiea/article/details/17092413
一.大众机型常用meta标签name的设置 1.name之viewport <meta name="viewport" content=""> 说明 ...
- CentOS tripwire-文件指纹
Tripwire是目前最为著名的unix下文件系统完整性检查的软件工具,这一软件采用的技术核心就是对每个要监控的文件产生一个数字签名,保留下来.当文件现在的数字签名与保留的数字签名不一致时,那么现在这 ...
- jquey :eq(1)
$("#div_Goods .datagrid-row .numberbox:eq(1)") $("#div_Goods .datagrid-row .numberbox ...
- python常用类型的内置函数列表
1.list.append(obj) 向列表中加入一个对象obj fruits = ['apple', 'pear', 'orange'] >>> fruits.ap ...
- DP:树DP
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Javascript设计模式系列二
创建对象的基本模式,一.门户大开型,二.采用下划线来表示属性和方法的私用性,三.使用闭包来创建私用的成员. 一.门户大开型.只能提供公用成员.所有属性和方法都公开的.可访问的.这些共用属性都要使用th ...
- 使用 WPF 实现所见即所得HTML编辑器
Introduction In this tip, you will learn the use of WPF webbrowser control and the use of the librar ...