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 ...
随机推荐
- 浅析深度学习mini_batch的BP反传算法
在深度学习中,如果我们已经定义了网络,输入,以及输出,那么接下来就是损失函数,优化策略,以及一般由框架完成的BP反传.这篇博文我们主要探讨一下深度的BP反传算法(以梯度下降为例),尤其是mini_ba ...
- Android的Intent机制详解
Intent 是一个消息传递对象,您可以使用它从其他应用组件请求操作.尽管 Intent 可以通过多种方式促进组件之间的通信,但其 基本用例主要包括以下三个: 启动 Activity: Activit ...
- UE4使用第三方库读写xml文件
原文链接:http://gad.qq.com/article/detail/7181031 本文首发腾讯GAD开发者平台,未经允许,不得转载 在游戏开发过程中,读写xml几乎已经成为不可或缺的功能,但 ...
- 一个貌似比较吊的递归转换为loop--总算成功了.--第二弹
前段时间用类似于散弹式编程的方式,各种猜测-运行验证-修正结果,最终成功转换了一个看起来比较有难度的递归函数.但总觉得很蛋疼,原因如下: 1.虽然正确,但是逻辑搞得比较复杂.现在去看,一头雾水,不知道 ...
- Swift如何取得View所属的ViewController
从VC取得View很容易,但有些情况下我们需要从View反向获取VC. 不过在一些特殊的场合,Cocoa库帮我们想的很周到,比如在自定义View过渡动画的时候: func animateTransit ...
- EJB_开发消息驱动bean
开发消息驱动bean Java消息服务(Java MessageService) Java 消息服务(Java Message Service,简称 JMS)是用于访问企业消息系统的开发商中立的API ...
- 自制DbHelper实现自动化数据库交互
之前一直对apache的DbUtils很好奇,也很佩服其中的设计上的智慧.于是就自己模拟实现了一个更加简便的小框架.我们只需要在配置文件中写上数据库层面的连接信息,就可以随心所欲的实现自己的需求了. ...
- SQL Server 执行计划操作符详解(3)——计算标量(Compute Scalar)
接上文:SQL Server 执行计划操作符详解(2)--串联(Concatenation ) 前言: 前面两篇文章介绍了关于串联(Concatenation)和断言(Assert)操作符,本文介绍第 ...
- android 获取SD卡的图片及其路径
1.首先是intent的设置: private static final int IMAGECODE = 0; Intent imageIntent = new Intent(Intent.ACYIO ...
- SSH网上商城---需求分析+表关系分析
SSH---小编初次接触的时候傻傻的以为这个跟SHE有什么关系呢?又是哪路明星歌手,后来才知道小编又土鳖了,原来SSH是这个样子滴,百度百科对她这样阐述,SSH即 Spring + Struts +H ...