【LeetCode每天一题】Rotate List(旋转链表)
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(旋转链表)的更多相关文章
- [LeetCode每日一题]153.寻找旋转排序数组中的最小值
[LeetCode每日一题]153.寻找旋转排序数组中的最小值 问题 已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组.例如,原数组 nums = [0,1, ...
- [LeetCode每日一题]81. 搜索旋转排序数组 II
[LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...
- [LeetCode] Rotate List 旋转链表
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 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 ...
- Leetcode61. Rotate List旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...
- leetcode腾讯精选练习之旋转链表(四)
旋转链表 题目: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = ...
- [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 ...
- 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 ...
- 061 Rotate List 旋转链表
给定一个链表,将链表向右旋转 k 个位置,其中 k 是非负数.示例:给定 1->2->3->4->5->NULL 且 k = 2,返回 4->5->1-> ...
随机推荐
- JS 判断传入的变量类型是否是Array
function f(arr){ 1.通过_proto_ 进行判断 (arr._proto_ 指向Array.prototype); 2.通过constructor进行判断 (arr.construc ...
- Java获取Ip发送邮件
import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.servlet. ...
- php隐藏手机号指定位数
function mobileReplace($mobile,$start,$end,$str="*"){ $countStr = abs($end-$start); $repla ...
- fastadmin表单验证
Unexpected token < in JSON at position 0 注意: if (!form.is("form"))//form的选择器不是form直接返回所 ...
- 几个简单排序算法的Python实现
一,冒泡排序 冒泡排序我就不多讲了,大体上就是比较相邻的两个数,每次把较大的数沉底.流程图大致上如下: 图是截得别人的,只是说明一下,代码没有参看别人的,写的不好,有更好的写法可以一起探讨.下面是代码 ...
- Python网络数据采集PDF
Python网络数据采集(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/16c4GjoAL_uKzdGPjG47S4Q 提取码:febb 复制这段内容后打开百度网盘手 ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Siberia
1. GUI 按题意判断即可. #include<stdio.h> #include<iostream> #include<string.h> #include&l ...
- svn提交按钮灰选
1.当我新建了一个文件或者文件夹,要提交的时候出现ok按钮灰选,提交不了. 解决方法:提交信息多写一些字儿就可以了,挥着回车换行也行 2.报错:you need to upgrade the work ...
- java遍历List中的map的几种方法
Student 类 public class Student { private String name; private int age; private int taller; public St ...
- Java三大集合框架
定义:java中集合类:是一种工具类,就像是容器,储存任意数量的具有共同属性的对象 一.List集合 1.List实现的超级父类接口:Collection 2.了解ArrayList类 A):定义的格 ...