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个开始后面的全部交换到链表的左侧。例如对于1,2,3,4,5,6 和 k = 2,就有交换之后的节点就是5,6,1,2,3,4。注意这里的k可能会超过链表的长度大小。

做法是首先可以计算链表的长度len,再将链表连成一个环状。接着从链表起始数len - k将链表断开,那么就完成了旋转,代码如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head) return NULL;
ListNode * p1 = head;
int count = ;
while(p1->next){
count++;
p1 = p1->next;
}
count++;
int len = count - k%count;//注意给出的k可能会超过链表的总长度
if(len == )
return head;
p1->next = head;
p1 = head;
for(int i = ; i < len; ++i){
p1 = p1->next;
}
ListNode * ret = p1->next;
p1->next = NULL;
return ret;
}
};

LeetCode OJ:Rotate List(旋转链表)的更多相关文章

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

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

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

  4. 【python】Leetcode每日一题-旋转链表

    [python]Leetcode每日一题-旋转链表 [题目描述] 给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置. 示例1: 输入:head = [1,2,3,4,5] ...

  5. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  6. 【LeetCode题解】61_旋转链表(Rotate-List)

    目录 描述 解法:双指针 思路 Java 实现 Python 实现 复杂度分析 描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1-> ...

  7. Leetcode61. Rotate List旋转链表

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  8. leetcode刷题-61旋转链表

    题目 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2输出: 4 ...

  9. 【LeetCode每天一题】Rotate List(旋转链表)

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  10. [LeetCode] 189. Rotate Array 旋转数组

    Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...

随机推荐

  1. $python用装饰器实现一个计时器

    直接上代码: import time from functools import wraps # 定义装饰器 def fn_timer(function): @wraps(function) def ...

  2. 【转】安装Vue.js的方法

    安装vue.js的方法   一.简介 Vue.js 是什么 Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量 ...

  3. linux ftp 简单搭建

    1.安装 yum install vsftpd 2.重启服务 /sbin/service vsftpd restartShutting down vsftpd: [ OK ]Starting vsft ...

  4. # 20145103《Java程序设计》第6周学习总结

    20145103<Java程序设计>第6周学习总结 教材学习内容总结 第十章 第十章输入和输出 10.1.1 ·若要将数据从来源中取出,可以使用输入串流:若要将数据写入目的地,可以使用输出 ...

  5. 20145216史婧瑶《Java程序设计》第三次实验报告

    实验三 敏捷开发与XP实践 实验内容 使用git上传代码,两个人进行小组合作,队友下载代码并修改再重新上传. 实验步骤 一. 使用git上传代码 1.找到需要push的文件所在文件夹,右键点击Git ...

  6. java中的垃圾回收机

    任何语言在运行过程中都会创建对象,也就意味着需要在内存中为这些对象在内存中分配空间,在这些对象失去使用的意义的时候,需要释放掉这些内容,保证内存能够提供给新的对象使用.对于对象内存的释放就是垃圾回收机 ...

  7. redis:Invalid input of type: 'bool' type. Convert to a byte,string or number first

    分析:出现此错误的原因是redis版本过高导致的,因此降低redis版本即可 解决: pip install -U redis==2.10.6

  8. Spring IOC 源码解析(持续)

    如何查看源码 Spring源码下载https://github.com/spring-projects/spring-framework/tags?after=v3.1.0.RC1 eclipse关联 ...

  9. SpringMVC封装表单数据

    1.domain类 package com.xiaostudy.domain; public class User { private int id; private String username; ...

  10. php入门(一)

    一,在HTML中嵌入php代码 先看html的代码: <form action="processorder.php" method="post"> ...