LeetCode61 Rotate List

题目:

Given a list, rotate the list to the right by k places, where k is non-negative. (Medium)

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

分析:

利用双指针,一个指针先走k步,然后两个指针同时走,runner走到链表尾时chaser在要翻转之前位置,然后折腾一下指针指向即可。

注意:如果k 大于指针长度时,题目表述不清,应该是按照循环回来处理。所以前面需要求一下链表长度,并把k % size。

代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* rotateRight(ListNode* head, int k) {
12         if (head == nullptr) {
13             return nullptr;
14         }
15         int size = 0;
16         ListNode* temp = head;
17         while (temp != nullptr) {
18             temp = temp -> next;
19             size++;
20         }
21         k = k % size;
22         ListNode* runner = head;
23         ListNode* chaser = head;
24         for (int i = 0; i < k; ++i) {
25             runner = runner -> next;
26         }
27         while (runner -> next != nullptr) {
28             runner = runner -> next;
29             chaser = chaser -> next;
30         }
31         runner -> next = head;
32         ListNode* result = chaser -> next;
33         chaser -> next = nullptr;
34         return result;
35     }
36 };
时间: 2024-10-27 03:31:24

LeetCode61 Rotate List的相关文章

CSS3 transform 属性详解(skew, rotate, translate, scale)

写这篇文章是因为在一个前端QQ群里,网友 "小豆豆" (应他要求要出现他的网名......) ,问skew的角度怎么算,因为他看了很多文章还是不能理解skew的原理.于是,我觉得有必要写个博文,帮助那些不懂的人,让他们看了此文就懂. 进入正题: 先说明下,电脑屏幕的XY轴跟我们平时所说的直角坐标系是不一样的.如下图: 图上的盒子就是代表我们的电脑屏幕,原点就是屏幕的左上角,竖直向下为X轴正方向,水平向右为Y轴正方向. 1.倾斜skew 先看图 每个图下方都有skew的参数.粗的红色的线

Leet Code OJ 189. Rotate Array [Difficulty: Easy]

题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can, there are at least 3 different ways to solve thi

leetcode 【 Rotate Image 】python 实现

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 代码:oj测试通过 Runtime: 53 ms 1 class Solution: 2 # @param matrix, a list of lists of integers 3 # @return a list

Rotate Array

LeetCode 189 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

CSS3 skew倾斜、rotate旋转动画

css3出现之前.我们实现一个对象的一组连续动画须要通过JavaScript或Jquery编写,脚本代码较为复杂: 若须要实现倾斜.旋转之类的动画难度将更高(我还没试过用JavaScript或Jquery怎样实现),并且即使能实现预计花的时间代价及维护难度是非常大的,非常多时候仅仅能依靠绘图工具制作此类动画文件: 有时候在想假设不用脚本语言,也不用绘图工作制作动画文件.就能在网页上实现倾斜.旋转之类的动画效果多好. 近期挤出一些业余时间学习CSS3,当中就包括非常多动画演示样例,花了点时间学习和

Rotate Image

参照计算机图形学图形变换即可. public class Solution { public void rotate(int[][] matrix) { if(matrix.length<=1)return ; float dip=(float) ((matrix.length-1)/2.0); int[][] res=new int[matrix.length][matrix.length]; for(int i=0;i<matrix.length;i++) for(int j=0;j<

How to Rotate Tomcat catalina.out

If catalina.out becomes 2GB in size, tomcat crashes and fails to start without any error message. To avoid this scenario you should rotate catalina.out frequently. This article describes how to setup auto rotation of catalina.out on a linux/unix mach

189. Rotate Array----Array----Easy----20160925

问题: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 思路2: 用一个额外的数组把数组完全复制过去 代码2: class Solution { public: void rotate(vector<int>& nums, int k) { i

【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 [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 这道题目 有很多种解法,我用的是这种,相对也好理解,先所有元素reverse,再0-(n-k)的元素reverse,然后(n-k)-n的元素reverse. class Solution { pub