leetcode[60] Rotate List
题目:给定链表,和一个k,把链表的后k个旋转到前头,例如链表为: 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.
一开始我想,跟将后面第k个元素删除一样,可以遍历一次就可以解决问题。但是在测评的时候发现k有大于链表长度的时候,我以为如果大于长度了就是不用翻转了,原来不是这样。如果k大于链表的长度了,那么就再从尾部开始往后一直到k为止,也就是我们可以看做在将k对链表长度取模之后的k才是一个链表从后往前的第k个。这样的话我们可以如下步骤做:
1.计算链表长度len
2.将k%=len
3.从后往前是第k个,那从前往后就是第len-k个,找到这个元素的前一个,然后再找到最后一个元素,将最后一个元素的next指向原来的head,然后将第k个元素设置为head,然后再讲第k-1个的next设置为NULL,大功告成。
/**
* 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 == NULL || k==) return head;
ListNode *right = head, *left = head;
int len = ;
while(right =right ->next) len++;
k %= len;
k = len - k;
right = head;
while(--k > ) right = right->next;
while(--len > ) left = left->next;
left->next = head;
head = right->next;
right->next = NULL;
return head;
}
};
leetcode[60] Rotate List的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [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] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- [Leetcode][048] Rotate Image 略详细 (Java)
题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有 ...
- LeetCode 189. Rotate Array (旋转数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- LeetCode 48. Rotate Image(旋转图像)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- LeetCode 48 Rotate Image(2D图像旋转问题)
题目链接: https://leetcode.com/problems/rotate-image/?tab=Description Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...
- 【LeetCode】 Rotate List 循环链表
题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a li ...
- [LeetCode] 189. Rotate Array 旋转数组
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
随机推荐
- HDU2451:Simple Addition Expression
Problem Description A luxury yacht with 100 passengers on board is sailing on the sea in the twiligh ...
- BCP导出导入
BCP导出导入大容量数据实践 前言 SQL SERVER提供多种不同的数据导出导入的工具,也可以编写SQL脚本,使用存储过程,生成所需的数据文件,甚至可以生成包含SQL语句和数据的脚本文件.各有优 ...
- Oracle 11g oracle客户端(32位)PL/SQL develepment的安装配置
Oracle 11g+oracle客户端(32位)+PL/SQL develepment的安装配置 之前一直想学Oracle,可是就是安装配置Oracle一直未成功,让人很苦恼,特别是什么监听器什么的 ...
- ubuntu下安装myeclipse
一.下载myeclipse 官网下载:http://www.myeclipseide.com/ 我使用的是myeclipse pro 2014.run,重命名为myeclipse.run 示例路径:/ ...
- vim note(3)
Ctrl+w Ctrl+v will create a new window on the right side of the current window Ctrl+w Ctrl+s wi ...
- SQL Server的还原
原文:SQL Server的还原 1.差异备份的还原 不备份结尾日志的情况下还原数据 创建差异备份的放在我们已经在前面一篇博客SQL Server的备份中提到了,这里我们不再赘述,下面我们给出差异备份 ...
- 第15章 迭代器模式(Iterator Pattern)
原文 第15章 迭代器模式(Iterator Pattern) 迭代器模式(Iterator Pattern) 概述: 在面向对象的软件设计中,我们经常会遇到一类集合对象,这类集合对象的内部结构 ...
- crawler_网络爬虫之数据分析_httpwatcher
所谓爬虫,首先要通过各种手段爬取到想要站点的数据. web2.0之后,各种网络站点类型越来越多,早期的站点多为静态页面[html .htm],后来逐步加入 jsp.asp,等交互性强的页面.再后来随着 ...
- 基于Linux平台病毒BlackHole病毒的决心
今天会见了病毒,少量的代码,但在使用小漏洞的函数的,真正的杀伤力相当惊人. 转载请注明出处:http://blog.csdn.net/u010484477谢谢^_^ watermark/2/text/ ...
- HDU 2516 取石子游戏 (博弈论)
取石子游戏 Problem Description 1堆石子有n个,两人轮流取.先取者第1次能够取随意多个,但不能所有取完.以后每次取的石子数不能超过上次取子数的2倍.取完者胜.先取者负输出" ...