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
.
原题链接: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的更多相关文章
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
- [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 ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [LeetCode] Rotate Image 旋转图像
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode]Rotate Image(矩阵旋转)
48. Rotate Image Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...
- [leetcode]Rotate List @ Python
原题地址:https://oj.leetcode.com/problems/rotate-list/ 题意: Given a list, rotate the list to the right by ...
- [leetcode]Rotate Image @ Python
原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representin ...
- 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 = ...
- [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 ...
随机推荐
- 重操JS旧业第六弹:基本类型包装
在前面已经知道js中的类型有boolean,string,number,undefined,function,object,其中boolean,number,string为值类型.所谓的基本类型包装, ...
- Qt 中文乱码解决大全
源地址:http://blog.csdn.net/xcy2011sky/article/details/7168376 解决中文乱码,最好知道乱码是什么格式比如说:utf-8. 解决方案: 1.让整个 ...
- QModelIndex有internalPointer()函数,可以存任何数据,另有QAbstractItemModel::createIndex来创造节点
整个model的节点数据,都靠它来记录了. 另有一个创造节点的函数(自带函数): QModelIndex QAbstractItemModel::createIndex(int arow, int a ...
- myeclispe启动后报错 Subclipse talks to Subversion via a Java API that requires access to native libraries.
myeclispe 中SVN插件常遇到的异常: Subclipse talks to Subversion via a Java API that requires access to native ...
- 安装m2eclipse
Help->Eclipse Marketplace- 搜索 maven 安装 Maven Integration for Eclipse
- 如何编译tizen源码(图文教程)?
前一篇文章已经介绍了如何下载tizen源码,下面我将继续讲述如何编译源码. 1 下载安装gbs编译工具 tizen源码是用gbs工具进行编译的,因此我们首先得将此工具下载下来,并且设置好. 下面的Ub ...
- [置顶] 64位Win2008_VS2012使用ODP.NET遭遇问题和解决办法
最近为使用Oracle11G数据库做个快速开发的小程序,使用64位Win2008+Vs2012环境,结果碰壁连环,幸好不算太笨,终于解决了,特记录一下. 测试环境: Oracle11g (11.2.0 ...
- UVA 6480 Zombie Invasion(模拟退火)
A group of survivors has arrived by helicopter to an isolated island. The island is made up of a lon ...
- JDBC使用数据库来完成分页功能
本篇讲诉如何在页面中通过操作数据库来完成数据显示的分页功能.当一个操作数据库进行查询的语句返回的结果集内容如果过多,那么内存极有可能溢出,所以在大数据的情况下分页是必须的.当然分页能通过很多种方式来实 ...
- 【测试】这是用微软word发布的博客
参考文章: http://blog.163.com/dsp163@126/blog/static/795585732011573383290/ 试试图片, 美女镇楼: