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 ,旋转列表最后 k 个元素至列表最前面。

关键是找到最后元素 lastOne 和 旋转后列表新的最后元素 newLastOne

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

         if (head == NULL) {
return NULL;
} int n = ;
ListNode* lastOne = head;
while (lastOne->next != NULL) {
n++;
lastOne = lastOne->next;
} if (n == k) {
return head;
}   int firstNum = n - (k % n); ListNode* newLastOne;
newLastOne = head;
for (int i = ; i < firstNum; i++) {
newLastOne = newLastOne->next;
} lastOne->next = head;
head = newLastOne->next;
newLastOne->next = NULL; return head;
}

[LeetCode] 61. Rotate List 解题思路的更多相关文章

  1. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  2. [LeetCode] Longest Valid Parentheses 解题思路

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. [LeetCode] 134. Gas Station 解题思路

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  4. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  5. [LeetCode] 16. 3Sum Closest 解题思路

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  6. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. [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 ...

  8. leetCode 61.Rotate List (旋转链表) 解题思路和方法

    Rotate List  Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...

  9. [Leetcode] Backtracking回溯法解题思路

    碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大 ...

随机推荐

  1. laravel实现数据库读写分离配置或者多读写分离配置

    config\database.php里 读写分离:'mysql' => array( 'read' => array( 'host' => '192.168.1.1', ), 'w ...

  2. WinSock IO模型 -- WSAEventSelect模型事件触发条件说明

    FD_READ事件 l  调用WSAEventSelect函数时,如果当前有数据可读 l  有数据到达时,并且没有发送过FD_READ事件 l  调用recv/recvfrom函数后,仍然有数据可读时 ...

  3. POJ3750

    #include <iostream> #include <stdio.h> #include <cstring> using namespace std; int ...

  4. .net中Web.config文件的基本原理及相关设置

    11.7  使用web.config配置文件 Web配置文件web.config是Web 应用程序的数据设定文件,它是一份 XML 文件,内含 Web 应用程序相关设定的 XML 标记,可以用来简化  ...

  5. 持续集成环境(Gitlab+jenkins+shell)

    一.搭建gitlab ps:不是这方面的专家,主要还是一键式安装为主. 1.进入官网:https://about.gitlab.com/gitlab-com/ 2.选择自己的操作系统:我这边选择的ub ...

  6. 武汉科技大学ACM :1009: 华科版C语言程序设计教程(第二版)习题6.11

    Problem Description n个人围成一圈,依次从1至n编号.从编号为1的人开始1至k报数,凡报数为k的人退出圈子,输出最后留下的一个人原来的编号. Input 首先输入一个t,表示有t组 ...

  7. 使用shiro安全框架上传文件时用HttpSession获取ServletContext为null问题解决方法。

    <!--在shiroFilter 中加入一下配置--> <init-param> <param-name>targetFilterLifecycle</par ...

  8. 数据库(批处理, 事务,CachedRawSetImpl类

    链接对象son产生的Statement SQL对象对数据库提交的任何一条语句都会被立刻执行 不方便我们进行一些连招操作 我们可以关闭它的自动提交,然后操作完再开,这过程称作事务 con.setAuto ...

  9. C/C++默认浮点型

    代码: #include <iostream> #include <cstdio> using namespace std; void test(int a){ cout< ...

  10. 关于js闭包杂记

    闭包:一个函数oneF里return了另一个函数innerF,然后在oneF外面运行了函数innerF,如果innerF里有用到在oneF里定义的变量,则此时依然可以引用到, 但是变量值不是定义函数i ...