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
.
比较直观的一个解法就是先求出链表长度,然后再确定从头节点位移到分割节点处的步数。接下来就是分割链表和拼接了。具体程序(8ms)如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head || !head->next || k == )
return head; int len = ;
ListNode *start = head;
while(start)
{
len++;
start = start->next;
}
k = len - k % len;
if(k == || k == len)
return head; ListNode *dummy = new(nothrow) ListNode(INT_MIN);
assert(dummy); start = head;
for(int i = ; i < k - ; i++)
start = start->next; dummy->next = start->next;
start->next = nullptr; start = dummy;
while(start->next)
start = start->next;
start->next = head; head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};
这题还有另一个很巧妙的解法:首先从head开始跑,直到最后一个节点,这时可以得出链表长度len。然后将尾指针指向头指针,将整个圈连起来,接着往前跑len – k%len,从这里断开,就是要求的结果了。
具体程序(12ms)如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head || !head->next || k == )
return head; int len = ;
ListNode *start = head;
while(start->next)
{
len++;
start = start->next;
}
k = len - k % len;
if(k == || k == len)
return head; start->next = head;
for(int i = ; i < k; i++)
start = start->next; head = start->next;
start->next = nullptr; return head;
}
};
LeetCode之“链表”:Rotate List的更多相关文章
- LeetCode 单链表专题 (一)
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- Leetcode解题-链表(2.2.0)基础类
1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø 规定好每个子Solution都要实现纯虚函数test做测试: Ø 提供了List ...
- 【算法题 14 LeetCode 147 链表的插入排序】
算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...
- LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...
- LeetCode OJ: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 ...
- [Swift]LeetCode61. 旋转链表 | Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
随机推荐
- frameset 与frame 设置的技巧
今天来写点不一样的.如下图: 实现的效果就是原生的类似于导航形式的frameset. frameset 注意: 包含frameset的网页应该只是作为框架而存在,所以不能有body标签. 这个标签可以 ...
- 1.2、Android Studio为新设备创建一个模块
模块为你的应用的源码.资源文件和app level设置(比如AndroidManifest.xml)提供了一个容器.每个模块可以独立的构建.测试和调试. 通过使用模块,Android Studio可以 ...
- shell-----sed命令详解
Table of Contents 1. Sed简介 2. 定址 3. Sed命令 4. 选项 5. 元字符集 6. 实例 7. 脚本 1. Sed简介 sed是一种在线编辑器,它一次处理 ...
- JS 遍历对象 jQuery遍历对象
jquery for 循环遍历对象的属性: //对象的定义如下: var person={id:"1",name:"springok",age:25}; for ...
- Linux系统编程----孤儿进程
什么是孤儿进程? 孤儿进程, 指在父进程退出后,而子进程还在运行,这个子进程就成了孤儿进程,这时由init进程(pid=1)接管 来看看例子: #include <stdio.h> #i ...
- octave installation on RHEL6.4
octave installation on RHEL6.4 rhel6.4上安装octave GNU Octave 是一种高级语言,主要设计用来进行数值计算,它是 MathWorks 出品的 Mat ...
- linux常用的内核镜像格式
linux常用的内核镜像格式 Linux内核有多种格式的镜像,包括vmlinux.Image.zImage等. 1. Linux内核镜像格式 1.1 vmlinux vmlinuz是可引导的. ...
- 06 intent flag三种属性
flag属性可以看做和写在清单文件中的启动模式一样 但效果有一定差别 1,FLAG_ACTIVITY_SINGLE_TOP:启动模式里的SingleTop一致 如果X启动模式设置为FLAG_ACTI ...
- 深入剖析Tomcat类加载机制
1JVM类加载机制 JVM的ClassLoader通过Parent属性定义父子关系,可以形成树状结构.其中引导类.扩展类.系统类三个加载器是JVM内置的. 它们的作用分别是: 1)引导类加载器:使用n ...
- RAMCloud:内存云存储的内存分配机制
现在全闪存阵列已经见怪不怪了,EMC的XtremIO,还有VNX-F(Rockies),IBM FlashSystem.全闪存真正为效率而生,重新定义存储速度.凭借极致性能,高可用性,为您极大提高企业 ...