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

Example 1:

Input: 1->2->3->4->5->NULL, k = 2               Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:Input: 0->1->2->NULL, k = 4 Output: 2->0->1->NULL

思路


  对于链表类的题目最重要的就是指针的控制,因此对于这道题我的思路就是我们先找到倒数第K个节点前一个节点的位置,并先将链表尾部指针指向头指针,然后将头指针指向倒数第K个节点,最后对倒数K节点上一个指针赋值为None。时间复杂度为O(n), 空间复杂度为O(1)

图示步骤


解决代码


 class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if not head or k < 0: # 为空或者K小于0直接返回
return head
length, tem = 0, head while tem: # 求出链表的长度
tem, length = tem.next, length + 1 k = k % length # 防止K的长度大于链表的长度
fast, slow = head, head
while k > 0: # 快指针先走K步
fast, k = fast.next, k-1 while fast.next: # 两个指针同时动
slow, fast = slow.next, fast.next
fast.next = head # 进行指针交换操作得到结果
head = slow.next
slow.next = None
return head

【LeetCode每天一题】Rotate List(旋转链表)的更多相关文章

  1. [LeetCode每日一题]153.寻找旋转排序数组中的最小值

    [LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...

  2. [LeetCode每日一题]81. 搜索旋转排序数组 II

    [LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...

  3. [LeetCode] Rotate List 旋转链表

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

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

  5. Leetcode61. Rotate List旋转链表

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

  6. leetcode腾讯精选练习之旋转链表(四)

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

  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. 061 Rotate List 旋转链表

    给定一个链表,将链表向右旋转 k 个位置,其中 k 是非负数.示例:给定 1->2->3->4->5->NULL 且 k = 2,返回 4->5->1-> ...

随机推荐

  1. sass进阶—变量运算

    /*变量操作 (两个变量之间的运算符需要用空格隔开,否则会报错.)==,!= <,>,<=,>=+,-,*,/,% */ $width1:50px;$width2:100px; ...

  2. Python学习笔记四

    一.装饰器 1.知识储备 函数对象 函数可以被引用 函数可以当参数传递 返回值可以是函数 可以当作容器的元素 def func1(): print (666) def func2(): print ( ...

  3. Java+maven+httpcomponents封装post/get请求

    httpcore4.4.10, httpclient4.5.6 package com.test.http; import com.alibaba.fastjson.JSONArray; import ...

  4. Mongodb字段自增长

    MongoClient client = new MongoClient("mongodb://xxx.xxx.x.xx:27017"); var mongServer = cli ...

  5. js分析 邮箱地址加密 [email protected]

    0.参考 https://segmentfault.com/q/1010000000117476 javascript里function之前加上感叹号 ' ! ' 会怎么样? // 这么写会报错,因为 ...

  6. 我的第二本译作《精通OpenStack》上架啦:书籍介绍和译者序

    1. 书籍简介 英文书名:Mastering OpenStack Second Edition 作者:[德] 奥马尔-海德希尔(Omar Khedher)[印] 坚登-杜塔-乔杜里(Chanda Du ...

  7. 【C#】时间类型修改

    鉴于前后端分离发展的迅速.前端很多时间控件都会读UTC时间. 安利一个小知识 // // 摘要: // Creates a new System.DateTime object that has th ...

  8. bzoj 4842 [Neerc2016]Delight for a Cat 最小费用最大流,线性规划

    题意:有n个小时,对于第i个小时,睡觉的愉悦值为si,打隔膜的愉悦值为ei,同时对于任意一段连续的k小时,必须至少有t1时间在睡觉,t2时间在打隔膜.如果要获得的愉悦值尽 量大,求最大的愉悦值和睡觉还 ...

  9. 几个简单排序算法的Python实现

    一,冒泡排序 冒泡排序我就不多讲了,大体上就是比较相邻的两个数,每次把较大的数沉底.流程图大致上如下: 图是截得别人的,只是说明一下,代码没有参看别人的,写的不好,有更好的写法可以一起探讨.下面是代码 ...

  10. CXF安装和配置时出现Exception in thread "main" java.lang.UnsupportedClassVersionError:异常?

    异常信息: C:\Users\>wsdl2java -h Exception in thread "main" java.lang.UnsupportedClassVersi ...