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 ...
随机推荐
- TDirectory.CreateDirectory 完整、严谨的创建一个目录
描述:创建一个目录,不包含多级目录(多级目录使用System.SysUtils.ForceDirectories,Vcl.FileCtrl.ForceDirectories已过时) procedure ...
- How to changes to Table & EDT Relations[AX2012]
Well I hope everyone is having a fine week so far. Oh Wednesdays, the furthermost point between two ...
- WCF全面解析第二章 地址(Adress)
2.1 统一资源标识(URL) 2.1.1 Http/Https 2.1.2 Net.TCP 2.1.3 Net.Pipe WCF只将命名管道专门用于同一台机器的跨进程通信. 2.1.4 Net.Ms ...
- XSS获取cookie
在你服务器的html目录下创建joke文件夹: 在joke文件夹中创建joke.js 和joke.php joke.js 创建img标签,将它的src属性指向另一个脚本joke.php,这里关键的一点 ...
- 使用Powershell在Microsoft Azure中创建Virtual Machine
获取虚拟机镜像 PS C:\WINDOWS\system32> Get-AzureVMImage 仅获得虚拟机名 PS C:\WINDOWS\system32> (Get-AzureVMI ...
- Configure Database Mirroring
使用证书配置的镜像基本安装微软次序做就可以了 http://msdn.microsoft.com/zh-cn/library/ms191140.aspx 备份还原首先要转换成完全备份模式没什么好多说的 ...
- 说说iOS中的手势及触摸
一.响应链 在IOS开发中会遇到各种操作事件,通过程序可以对这些事件做出响应. 首先,当发生事件响应时,必须知道由谁来响应事件.在IOS中,由响应者链来对事件进行响应,所有事件响应的类都是UIResp ...
- Window.Open参数、返回值
一.window.open()支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二.基本语法: window.open(pageURL,name, ...
- Wireshark技巧-过滤规则和显示规则
Wireshark是一个强大的网络协议分析软件,最重要的它是免费软件. 过滤规则 只抓取符合条件的包,在Wireshark通过winpacp抓包时可以过滤掉不符合条件的包,提高我们的分析效率. 如果要 ...
- 【锋利的JQuery-学习笔记】Tootip(提示框)
效果图: 1.当鼠标移动到超链接时,有提示框. 2..当鼠标移动到图片动画旋转 html: <div id="jnNotice"> <div id="j ...