【Leetcode】【Medium】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.
解题思路:
题目看上去很简单,但是需要注意一下点:
1、任何输入都有可能,即使是无效的输入(例如链表为空,k不为0),所以程序开始时的检查在任何时候都非常重要。
2、另一个边界条件,就是是否在空指针中,使用了->next操作,很多链表错误都发生在此处。
3、K是旋转的结点数,因此K可能大于整个链表的结点数。
4、K不仅可能大于结点数,还可能非常大,因此传统的循环耗费时间太多,要对K做取余操作。
代码:
/**
* 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 (k == || !head)
return head;
ListNode* pre = head;
ListNode* last = head;
int num = ;
while (k--) {
if (last->next) {
num++;
last = last->next;
} else {
k = k % num;
last = head;
}
} while (last->next) {
pre = pre->next;
last = last->next;
} last->next = head;
head = pre->next;
pre->next = NULL;
return head;
}
}
【Leetcode】【Medium】Rotate List的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode从零单排】No189 .Rotate Array
称号 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...
- 【LeetCode每天一题】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 Image(旋转矩阵)
You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise). N ...
- 【leetcode刷题笔记】Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【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算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
随机推荐
- Jmeter断言实例—响应断言
断言有很多种,最最最常用的一种就是响应断言,目前我用的最多是这一种,下面列举一个运用响应断言的实例 对相应的请求添加断言 **Main sample and sub-samples:断言应用于主采样器 ...
- orcale 之 SQL 数据查询
从数据库中检索行,并允许从一个或多个表中选择一个或多个行或列.虽然 SELECT 语句的完整语法较复杂,但是其主要的子句可归纳如下: SELECT select_list [ INTO new_tab ...
- The Add-in 'VMDebugger' failed to load or caused an exception.....的解决办法
异常如图: 解决办法: (把VS的设置导出来,做出相应修改后,再导入,问题就可以解决了) 1. 在Visual Studio中选择 Tools --> Import and Export Set ...
- bzoj 2167: 公交车站
Description Z市交通不发达,所有公交路线覆盖的边竟然一个环也不包含,甚至该市的公交路线有可能会分为几个互不连通的块,这可真是不可思议.有一天,你突然听到一条消息,说你的M个同学被困在了Z市 ...
- 判断网站域名是否被GFW(墙)过滤屏蔽了
GFW:Greate Firewall Of China中国防火长城: 描述: 1.今天所属的一个域名被告诉不能访问了,赶紧自己测试了一下,发现可以,然后对方试了下说是不行,然后仔细按对方说的一步步操 ...
- [转]Oracle中没有 if exists(...)
本文转自:http://blog.csdn.net/hollboy/article/details/7550171 对于Oracle中没有 if exists(...) 的语法,目前有许多种解决方法, ...
- Java - 谨慎覆盖equals
平时很难遇到需要覆盖equals的情况. 什么时候不需要覆盖equals? 类的每个实例本质上是唯一的,我们不需要用特殊的逻辑值来表述,Object提供的equals方法正好是正确的. 超类已经覆盖了 ...
- Vue中的静态资源管理(src下的assets和static文件夹的区别)
### 你可能注意到了我们的静态资源共有两个目录src/assets和static/,你们它们之间有怎样的区别呢? 资源打包 为了回答这个问题,我们需要了解webpack是如何处理静态资源的. 在所有 ...
- ES6的新知识点
一.变量 原有变量: var的缺点: 1.可以重复声明 2.无法限制修改 3.没有块级作用域 新增变量: let :不能重复声明,变量-可以修改,块级作用域 const:不能重复声明,变量-不可以修改 ...
- 移动端适配(2)——viewport适配
通过viewport来适配 <script> (function(){ var w=window.screen.width; console.log(w);//获取屏幕尺寸 var tar ...