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 ...
随机推荐
- 解 自己关于 C# Button的Click事件的疑惑
先说说C#中事件的用法,从事件的用法中,我自己会产生一个疑惑 C#事件 class Program { static void Main(string[] args) { EventClass ec ...
- QT+vs2010下改变可执行程序的图标
原地址:http://blog.163.com/tijijun@126/blog/static/6820974520134209457308/ 在解决方案下面的工程里,点击右键 ->选择[添加( ...
- MFC模板CArray及其派生类
CArray及其派生类 1. 简介:访问方法及效率和普通的数组一样,比普通数组强大的功能是可以改变数组的大小.Array采用队列方式存储数据,因而其内部数据元素是以物理方式顺序排列的,所以检索.顺序执 ...
- Swift - 多线程实现方式(3) - Grand Central Dispatch(GCD)
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- Serialize a Binary Tree or a General Tree
For a binary tree, preorder traversal may be enough. For example, _ / \ / / \ 50 ...
- 使用JDBC调用数据库的存储过程
本篇讲述如何使用JDBC来调用MySQL数据库中的存储过程.建议在学习如何使用JDBC调用存储过程前,请先了解如何在数据库中使用存储过程. 存储过程是指在数据库系统中,一组为了完成特定功能的SQL语句 ...
- Windows串口编程
串口基础知识 http://www.cnblogs.com/menlsh/archive/2013/01/28/2880580.html DTU知识 http://blog.csdn.net/xuto ...
- 【Demo 0008】Java基础-抽象类
本掌学习要点: 1. 了解抽象类的定义及使用场景: 2. 掌握final修饰的类.方法以及变量作用及用法: 3. 掌握abstract 修饰的类及方法作用及用 ...
- uva 12627
题意:开始有1个红气球,每小时后1个红气球会变为3个红气球和1个蓝气球,问k小时后第A行到第B行的气球数. 解:用g(k,i)表示第k小时时,从底部数i行的红气球数.所以ans = g(k,2^k-A ...
- CRectTracker类的使用--橡皮筋窗口
CRectTracker(俗称“橡皮筋”类)是一个非常有意思的类.你在Windows中经常看到这样的情况:它可以用做显示边界,你也可以扽它的八个角用来放大缩小,或做框选使用.如何通过编程来实现这种功能 ...