LeetCode题解:Rotate List
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.
思路:
这个题目的陷阱中,输入为空指针大约不算其中之一。主要的问题是k可以大于这个链表的总长度。这样,真正的旋转次数是k % length(ListNode)。那么新的head就是length(ListNode) - k + 1的结点。
题解:
/**
* 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 == nullptr)
return nullptr; // first update the length of the list
ListNode* len_iter = head;
int len = 1;
while(len_iter->next != nullptr)
++len, len_iter = len_iter->next; k = k % len; if (k == 0)
return head; len_iter->next = head; // make it a circled link list
ListNode* breakpoint = head;
for(int i = 0; i < len - k - 1; ++i)
breakpoint = breakpoint->next; head = breakpoint->next;
breakpoint->next = nullptr; return head;
}
};
LeetCode题解:Rotate List的更多相关文章
- [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题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- [array] leetcode - 48. Rotate Image - Medium
leetcode - 48. Rotate Image - Medium descrition You are given an n x n 2D matrix representing an ima ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
- 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)
目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...
- 【LeetCode题解】232_用栈实现队列(Implement-Queue-using-Stacks)
目录 描述 解法一:在一个栈中维持所有元素的出队顺序 思路 入队(push) 出队(pop) 查看队首(peek) 是否为空(empty) Java 实现 Python 实现 解法二:一个栈入,一个栈 ...
- 【LeetCode题解】844_比较含退格的字符串(Backspace-String-Compare)
目录 描述 解法一:字符串比较 思路 Java 实现 Python 实现 复杂度分析 解法二:双指针(推荐) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以 ...
- 【LeetCode题解】25_k个一组翻转链表(Reverse-Nodes-in-k-Group)
目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记 ...
- 【LeetCode题解】24_两两交换链表中的节点(Swap-Nodes-in-Pairs)
目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度要求) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解 ...
随机推荐
- 【python学习-3】python数据类型
1.数字 在python 2.x中,数字类型有4种,int.long.float 和 complex(复数):而python 3 中,只有 int.float 和 complex 3种,python ...
- eclipse中的maven build、maven clean、maven install和maven test的区别
eclipse中的maven build.maven clean.maven install和maven test的区别 https://www.cnblogs.com/Marydon20170307 ...
- webshpere 节点&环境分析
场景名称:LotusConnections: 场景下有一个节点:feb02Node1 节点下有一个应用服务器 LotusConnections server.
- 五校联考R1 Day1T3 平面图planar(递推 矩阵快速幂)
题目链接 我们可以把棱柱拆成有\(n\)条高的矩形,尝试递推. 在计算的过程中,第\(i\)列(\(i\neq n\))只与\(i-1\)列有关,称\(i-1\)列的上面/下面为左上/左下,第\(i\ ...
- [CEOI2017]Palindromic Partitions
[CEOI2017]Palindromic Partitions 题目大意: 给出一个长度为\(n(n\le10^6)\)的只包含小写字母字符串,要求你将它划分成尽可能多的小块,使得这些小块构成回文串 ...
- hashMap归纳
Hashmap的与hashtable的区别: Hashmap:允许key为空:查询速度快(他是非同步的:避免了同步中不必要的判断):不安全的(容易引 发多线程安全问题) Hashtable:不允许k ...
- BZOJ 3245: 最快路线 spfa
3245: 最快路线 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3245 Description 精明的小R每每开车出行总是喜欢走最快 ...
- 使用 IntraWeb (6) - 页面模板: TIWLayoutMgrHTML、TIWTemplateProcessorHTML
IW 通过 TIWLayoutMgrHTML 和 TIWTemplateProcessorHTML 使用 HTML 模板. 所谓模板就是一个特殊 HTML 文件, 特殊之处是: 它里面会类似 {% I ...
- Digital adjustment of LM317
- tomcat开启SSL8443端口的方法
参考文献: http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html http://blog.sina.com.cn/s/blog_682b5aa1 ...