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. 《UNIX网络编程》之点对点通信

    思路: 点对点通信,其原理也比较简单,在前面回显服务器的基础上,我们分别在服务端和客户端都使用两个线程,一个线程负责发送数据包,一个线程负责接收数据包. 代码如下: 客户端: /*********** ...

  2. id名和class名有什么区别

    从概念上来说: id是先找到结构/内容,再给它定义样式: class是先定义好一种样式,再套给多个结构/内容. 从样式效果上来说:    id的优先级要比class高出一个层次 html中不管有几个i ...

  3. 解决 iOS webkit 使用CSS动画时闪烁的问题

    -webkit-backface-visibility: hidden;

  4. C#获取当前系统磁盘符、系统目录、桌面等

    1.获取方式如下 Environment.SpecialFolder中定义了许多常用的目录 //获取当前系统磁盘符方法1,返回:C: string path = Environment.GetEnvi ...

  5. PL/SQL客户端安装配置说明

    一.电脑安装了多个Oracle客户端时,需要设定pl/sql 中的home 二.配置环境变量: (打开环境变量配置界面操作:我的电脑---属性---高级---环境变量,在系统变量部分新建或编辑即可.w ...

  6. share js 分享代码

    (function(){ var $doc = $(document); var shareHandlers = { 'twitter': function(prop,shareUrl){ var D ...

  7. 【译】addEventListener 第二个参数

    这是原文链接:Our Backwards DOM Event Libraries 浏览器APIs 先简单回顾一下各个浏览器提供了哪些绑定事件的接口. IE浏览器提供了element.attachEve ...

  8. WPF画N角芒星,正N角星

    计算顶部三角形坐标方法: /// <summary> /// 获取顶三角形坐标 /// </summary> /// <param name="r"& ...

  9. Mybatis的学习总结(一)——使用配置文件实现增删改查

    在使用Mybatis作为持久层来进行操作数据库,有很多的操作都是一样的,基本上都是先得到session,然后调用session提供的相关方法进行操作,接着提交session,最后关闭session.那 ...

  10. 解决easyui datagrid加载数据时,checkbox列没有根据checkbox的值来确定是否选中

    背景:   昨天帮朋友做一个easyui datagrid的小实例时,才发现easyui datagrid的checkbox列,没有根据值为true或false来选中checkbox,当时感觉太让人失 ...