【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 ...
随机推荐
- 爬虫--requeste
1.requeste模块,是我们Python对我们爬虫有好的一面,,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半壁江山的地位.没有的话直接pip3 install ...
- oracle dump的使用心得
使用DS开发的时候,有的时候会遇到一个问题:数据库层面定义的空格与DS自已定义的空格概念不一致,导致生成的数据会有一定的问题. 举例来说: 在数据库里面定义CHAR(20),如果插入的字符不足20的时 ...
- Linux多个机器配置ssh免登陆
多机器ssh免密码登录的教程,网上有很多,多的数不过来,但是我的安装过程不是很顺利,因为刚开始使用的是普通的user,虽然配置了sudo权限,但是没有root权限,导致了无论如何配置都不能实现免密码登 ...
- 【随笔】nginx重启问题和mysql挂了的解决办法
租了一个阿里云服务器,然后需要一个nginx来处理一下静态文件的访问和动态文件的转发,头一天没有什么问题,第二次打开,各种问题就出来了!解决方法记录一下.... Can't connect to lo ...
- 关于CSS3动画性能
前天我去面试了...好吧,对于自己6年6份工作的悲催经历,我自己也是醉了. 但没办法,我这种当时上学没好好学习,临毕业才出家写代码的半吊子码农,起步没起好,以至于一直没能找到真正让自己满意的工作. 通 ...
- html的framset使用
frameset主要用在显示多个页面的需求下: 看代码: <html> <head> <title>html frameset test</title> ...
- linux下logrotate配置和理解---转
http://os.51cto.com/art/200912/167478_all.htm 对于Linux 的系统安全来说,日志文件是极其重要的工具.系统管理员可以使用logrotate 程序用来管理 ...
- 持续集成工具TeamCity配置使用
持续集成CI(Continuous Integration)主要包括自动化的编译.发布和测试集成,对于我们信息系统项目开发非常有用.一般开发人员机器上会搭建自己的开发环境,整个项目在服务器上会搭建测试 ...
- [PY3]——heap模块 和 堆排序
heapify( ) heapify()函数用于将一个序列转化为初始化堆 nums=[16,7,3,20,17,8,-1] print('nums:',nums) show_tree(nums) nu ...
- cut、grep和排序命令
1.cut 对于行进行操作 cut -d ':' -f 2 以':'为分隔符,切出第二部分的所有行 cut -c 12- 从第12字符往后的字符所有行 2.grep grep '选取的串' 选出所有含 ...