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 ...
随机推荐
- Delphi XE5 for Android(十一篇)
http://www.cnblogs.com/ChinaEHR/category/521326.html http://blog.csdn.net/laorenshen
- ORACLE DATABASE 10G FALSHBACK 知识整理
1.知识储备 1) 当出现介质损坏时(如数据文件丢失),任何闪回方法都毫无用处,只能执行标准的备份.还原与恢复. 2.SCN记录方法 SQL>variable x_scn number; ...
- CSDN改版问题多多
刚刚上CSDN,发现改版了,推出C币功能. 然后看了2分钟,发现了一个Bug,于是准备提交到论坛.但是--居然提交Bug的论坛也出现Bug.印象中,每次CSDN更新版本号Bug都非常多,这,作为程序猿 ...
- 给线程发送消息让它执行不同的处理(自己建立消息循环,非常有意思) good
unit Unit2; interface usesSystem.Classes, Windows, Messages; constWM_DO = WM_USER + 1; typeTDemoThre ...
- Android开发okhttp,retrofit,android-async-http,volley?
okhttp, retrofit,android-async-http,volley这四个框架适用的场合?优缺点?各位大大,请给一些建议.我准备开发一个新的APP 如果是标准的RESTful API, ...
- 10165 - Stone Game(Nim游戏)
UVA 10165 - Stone Game 题目链接 题意:给定n堆石子,每次能在一堆取1到多个.取到最后一个赢,问谁赢 思路:就裸的的Nim游戏,利用定理求解 代码: #include <s ...
- “聊天剽窃手”--ptrace进程注入型病毒
近日,百度安全实验室发现了一款"聊天剽窃手"病毒.该病毒可以通过ptrace方式注入恶意代码至QQ.微信程序进程.恶意代码可以实时监控手机QQ.微信的聊天内容及联系人信息. 该病毒 ...
- linux 配置 mail server
一.配置yum安装工具 ① 进入yum目录 [root@bj ~]# cd /etc/yum.repos.d ② 配置yum.repo [root@bj yum.repos.d]# cprhel- ...
- [破解]java打包Exe工具 - Jar2Exe Wizard
打包java文件为exe的方法和软件有很多,还有一些开源的软件和一些免费的软件. 我用过的所有打包exe软件中,Jar2Exe Wizard是最好用的,但是只有一个月的试用期,需要的可以从官网下载. ...
- #pragma详解
在#Pragma是预处理指令它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C ++语言完全兼容的情况下,给出主机或操作系统专有 ...