61. 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.
链接:http://leetcode.com/problems/rotate-list/
题解:
先遍历一次链表,将尾部和头部相连,再进行移动。注意右移k步相当于prehead travel len - k步。
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(head == null || k == 0)
return head;
ListNode node = head;
int len = 1;
while(node.next != null){
node = node.next;
len ++;
}
node.next = head;
k %= len;
for(int i = 0; i < len - k; i++)
node = node.next;
head = node.next;
node.next = null;
return head;
}
}
二刷:
主要思路还是把链表结成环。
- 先求出链表长度。
- 将链表结成环
- 计算k的合理范围。向右移动k位,就相当于从表头向左travel len - k个单位:
- 假如k < 0,那么说明向左移动,我们只需要计算-k % len就可以了
- 假如k > 0,我们需要把k变换为 len - k % len
- 接下来当k > 0的时候,我们从node继续往下travel, k--
- 第4步的遍历结束时,我们可以设置新的head = node.next,然后在此处断开环, node.next = null
- 最后返回head。
Java:
Time Complexity - O(n), Space Complexity - O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
ListNode node = head;
int len = 1;
while (node.next != null) {
node = node.next;
len++;
}
node.next = head;
if (k < 0) {
k = -k % len;
} else {
k = len - k % len;
}
while (k > 0) {
node = node.next;
k--;
}
head = node.next;
node.next = null;
return head;
}
}
三刷:
看到k是non-negative的我就放心了。这里要注意我们结成环以后,在链表尾部寻找新的表头,需要移动的距离是 len - k % len。这样node.next就是新的表头。
Java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || k <= 0) return head;
ListNode node = head;
int len = 1;
while (node.next != null) {
node = node.next;
len++;
}
node.next = head; k = len - k % len;
while (k > 0) {
node = node.next;
k--;
}
head = node.next;
node.next = null;
return head;
}
}
61. Rotate List的更多相关文章
- 61. Rotate List(M);19. Remove Nth Node From End of List(M)
61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- Leetcode#61 Rotate List
原题地址 我一直不太理解为什么叫rotate,翻译成"旋转"吧,似乎也不像啊.比如: 1->2->3->4->5->NULL 向右旋转2的距离,变成了 ...
- [LeetCode] 61. Rotate List 解题思路
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- LeetCode OJ 61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【一天一道LeetCode】#61. Rotate List
一天一道LeetCode系列 (一)题目 Given a list, rotate the list to the right by k places, where k is non-negative ...
- [leetcode]61. Rotate List旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 61. Rotate List(List)
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
随机推荐
- easyui菜单栏的使用
<div id="tabs" class="easyui-tabs" data-options="plain:true,fit:true,bor ...
- ActiveMQ之Queue
Queue实现的是点到点模型,在以下的例子中,启动2个消费者共同监听一个Queue,然后循环给这个Queue发送多个消息. 代码如下: public class QueueTest { /** * @ ...
- [ios]ios的延迟执行方法
1.最直接的方法performSelector:withObject:afterDelay: 这种方法的缺点:每次要为延时写一个方法 2.使用类别,用BOLCK执行 [代码]c#/cpp/oc代码 ...
- php 彩票类 lottery
<?php /* * For the full copyright and license information, please view the LICENSE * file that wa ...
- Extjs ajax form 提交
1.form 提交 form.form.submit({ url: "/HandlerExcelToDB/UploadFile.ashx", params: {}, success ...
- [转载]iOS面试题总
转载自:http://blog.sina.com.cn/s/blog_67eb608b0101r6xb.html (2014-06-13 20:23:33) 转载▼ 标签: 转载 crash 原文 ...
- D3js
http://d3js.org http://blog.csdn.net/lzhlzz/article/details/27497157
- cocos2dx中的动作
CCAction是cocos2dx中专门用来处理动作相关的类,几乎所有的与动作相关的类都是从它派生而来的.而CCAction继承自CCObject class CCFiniteTimeAction : ...
- php中的性能挖掘
搞php以后,感觉总是很别扭,因为我觉得php会很慢,因为array普遍,在Key的循环查找不是很浪费性能么!因为我以前搞.net和java,他们是用的大多是寻址和索引方式,而php中太多是使用Key ...
- 01-05-01-1【Nhibernate (版本3.3.1.4000) 出入江湖】延迟加载及其class和集合(set、bag等)的Lazy属性配置组合对Get和Load方法的影响
这篇文章 http://ayende.com/blog/3988/nhibernate-the-difference-between-get-load-and-querying-by-id One o ...