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 ...
随机推荐
- 深入浅出OpenStack云计算平台管理(nova-compute/network)
一.本课程是怎么样的一门课程(全面介绍) 1.1. 课程的背景 OpenStack是 一个由Rackspace发起.全球开发者共同参与的开源项目,旨在打造易于部署 ...
- php 实时汇率接口
function getExchangeRate($from_Currency,$to_Currency) { $amount = urlencode($amount); $from_Currenc ...
- ftk学习记(对话框篇)
[声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 前面谈到了输入法,首先看一看效果. 上面有4个输入框,大家能够分别试试,看看效果怎样. 今天,我 ...
- 0 and 1
Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think ab ...
- May Day Holiday
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status Practic ...
- POJ 3017 单调队列dp
Cut the Sequence Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8764 Accepted: 2576 ...
- kgdb接收一个数据包详解
0 kdb>kgdb // 可进入kgdb 模式 if (dbg_kdb_mode) { error = kdb_stub(ks); } else ...
- Excel设置下拉选项的方法
前些日子参加提高班组织的数据采集工作,到各个二级学院搜集数据,当然离不开我们常用的Excel表格了.在这次采集数据的过程过程中还真学到了一两招.就比如在Excel中设置下拉选项的方法. 例如我们要在A ...
- 让VC2012生成的程序支持XP系统(QT的DLL都是支持XP的,只与EXE有关)
如果用的编译器是VC2012以上,那么默认生成出的程序是不能运行在XP系统上的.所以需要修改链接参数 我们要做的是修改qmake.conf文件中的参数,文件路径根据开发环境不同而不同下面以5.1.1 ...
- perl lwp 获取请求头
<pre name="code" class="html">[root@dr-mysql01 ~]# cat getx.pl use LWP::Us ...