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. 怎样绕过oracle listener 监听的password设置

     怎样绕过oracle 监听的password设置: 1.找到监听进程pid ,并将它kill 掉 ps -ef|grep tns [oracle@lixora admin]$ ps -ef|gr ...

  2. STL之set && multiset

    一.set 在了解关联容器set之前,让我们先来看看下面这个例子,并猜测该例子输出什么: // stl/set1.cpp #include <iostream> #include < ...

  3. 开发部署一个简单的Servlet

    Servlet是一个执行在服务器端的Java Class文件,载入前必须先将Servlet程序代码编译成.class文件,然后将此class文件放在servlet Engline路径下.Servlet ...

  4. MySQL安装与测试

    工作室老师要求我们把MySQL装出来 今天折腾了下,本来不难的,不知道为什么用最新5.6.24的msi安装包,安装的时候选完路径后就没有后续了..蛋疼的我试了好几次,用cmd命令测试一直是 2003- ...

  5. mockServer学习

    mockServer学习 很喜欢mockserver官方主页的背景颜色和格式 官方主页如下: http://www.mock-server.com/

  6. [php基础]PHP Form表单验证:PHP form validator使用说明

    在PHP网站开发建设中,用户注册.留言是必不可少的功能,用户提交的信息数据都是通过Form表单提交,为了保证数据的完整性.安全性,PHP Form表单验证是过滤数据的首要环节,PHP对表单提交数据的验 ...

  7. 星座物语APP

    效果图: 这里的后台管理用的是duducat,大家可以去百度看说明.图片,文字都在duducat后台服务器上,可以自己修改的.(PS:图片这里是随便找的) http://www.duducat.com ...

  8. android SQLite使用SQLiteOpenHelper类对数据库进行增删查改

    一个简单的例子,当点击按钮时进行相应的操作,效果图如下: 项目代码如下: DatabaseHelper类 package com.example.sqlitedatebasetest; import ...

  9. Win7系统安装MySQL

    最近重装系统,重新搭建编译环境:重装mysql,发现一篇特别好的安装博客(http://blog.csdn.net/longyuhome/article/details/7913375),转载过来,留 ...

  10. JQuery 中的Ajax

    JQuery 对 Ajax 操作进行了封装, 在 jQuery 中最底层的方法时 $.ajax(), 第二层是 load(), $.get() 和 $.post(), 第三层是 $.getScript ...