leetcode || 61、Rotate List

problem:

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.

Hide Tags

Linked List Two
Pointers

题意:将倒数总数K个结点反转到前面,研究了一阵子才搞懂题意,醉了...

thinking:

(1)链表的反转问题,双指针解决,一个前驱指针+前进指针

(2)注意K的大小

code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int calListLen(ListNode *node)  //计算链表长度
    {
        int len = 0;
        while(node)
        {
            len++;
            node = node->next;
        }
        return len;
    }

    ListNode *rotateRight(ListNode *head, int k) {

        if (head == NULL)
            return NULL;

        int len = calListLen(head);

        k = k % len;   //K有可能大于链表长度

        if (k == 0)
            return head;

        ListNode *p = head;
        ListNode *pPre = NULL;

        for(int i = 0; i < len - k; i++)
        {
            pPre = p;              //前驱
            p = p->next;           //反转点第一个结点
        }

        ListNode *q = p;
        while(q->next)     //到达最后一个结点
            q = q->next;

        if (pPre)
            pPre->next = NULL;

        q->next = head;

        return p;
    }
};
时间: 2024-08-03 04:31:38

leetcode || 61、Rotate List的相关文章

leetcode || 48、Rotate Image

problem: 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? Hide Tags Array 题意:将一个矩阵顺时针旋转90度 thinking: (1)题目要求原址操作 (2)先将矩阵 转置,再将矩阵沿中间对称左右翻转 code: class Solution {

LeetCode --- 61. Rotate List

题目链接: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. 这道题的要求是向右旋转链表k步. 其实就是把链表后面l-k个节点放到前面,可以采用快慢指针处理.不

CSS3 skew倾斜、rotate旋转动画

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

LeetCode:(Array-189) Rotate Array

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]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to s

自定义控件三部曲之动画篇(一)——alpha、scale、translate、rotate、set的xml属性及用法

前言:这几天做客户回访,感触很大,用户只要是留反馈信息,总是一种恨铁不成钢的心态,想用你的app,却是因为你的技术问题,让他们不得不放弃,而你一个回访电话却让他们尽释前嫌,当最后把手机号留给他们以便随时沟通的时候,总会发来一条条的鼓励短信,让我不自主的开始内疚.哎,多么可爱的用户,多么无耐的现实. 相关文章: <Android自定义控件三部曲文章索引>:http://blog.csdn.net/harvic880925/article/details/50995268 一.概述 Android

leetcode || 118、Pascal&#39;s Triangle

problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

matrix()方法与translate()、scale()、rotate()、skew()方法的关系

2D变换方法translate().scale().rotate().skew()与matrix()的关系举例介绍. 一.介绍 2D变换方法: translate():根据左(X轴)和顶部(Y轴)位置给定的参数,从当前元素位置移动. rotate():在一个给定度数顺时针旋转元素.负值是允许的,这样是元素逆时针旋转.(绕着中心点旋转) scale():元素按比例缩放,比例取决于宽度(X轴)和高度(Y轴)的参数.(缩放功能与中心点的位置有关) skew():X轴和Y轴倾斜一定角度. matrix(

Android自定义控件:动画类----alpha、scale、translate、rotate、set的xml属性及用法

一.概述 Android的animation由四种类型组成:C.scale.translate.rotate,对应android官方文档地址:<Animation Resources> 动画在XML配置文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面转移旋转动画效果 动作定义文件应该存放在res/anim文件夹下,访问时采用R.anim.XXX.xml的方式,位置如图: 二.下面我们逐个讲讲每个标签的属性

leetcode || 119、Pascal&#39;s Triangle II

problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间