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: ...
随机推荐
- [ACM] poj 1088 滑雪 (内存搜索DFS)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 73409 Accepted: 27141 Description ...
- Git Config(转)
一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境. 你只需要做这些设置一次:即使你升级了,他们也会绑定到你的环境中.你也可以在任何时刻通过运行命令来重新更改这些设置. ...
- JDK的dt.jar和Java BeanInfo接口
在JAVA_HOME/lib以下有两个比較重要的jar文件.tools.jar和dt.jar. tools.jar在上篇文章中做了简单的介绍.这里来介绍下dt.jar. 在Oracle官方站点搜dt. ...
- Chromium on Android: Android在系统Chromium为了实现主消息循环分析
总结:刚开始接触一个Chromium on Android时间.很好奇Chromium主消息循环是如何整合Android应用. 为Android计划,一旦启动,主线程将具有Java消息层循环处理系统事 ...
- 【日报C在23】堆和栈的深入了解
每日一C之堆与栈的深入理解 每天拾一个C语言贝壳,厚积薄发,积跬步以致千里. 今日贝壳:内存中堆与栈的深入理解.认识一个清晰地内存 假 ...
- UVALive 4730 Kingdom +段树和支票托收
主题链接:点击打开链接 题意见白书P248 思路: 先把读入的y值都扩大2倍变成整数 然后离散化一下 用线段树来维护y轴 区间上每一个点的 城市数量和联通块数量. 然后用并查集维护每一个联通块及联通块 ...
- OCP读书笔记(23) - 题库(ExamC)
200.Which operation requires that you create an auxiliary instance manually before executing the ope ...
- Custom Data Service Providers
Custom Data Service Providers Introduction Data Services sits above a Data Service Provider, which i ...
- 深入探讨 Java 类加载器[转]
原文地址:http://www.ibm.com/developerworks/cn/java/j-lo-classloader/index.html 类加载器(class loader)是 Java™ ...
- 重写ArcGIS的TiledMapServiceLayer呼叫世界地图图块
require(["esri/layers/TiledMapServiceLayer"], function () { dojo.declare("com.Str ...