【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-> ...
随机推荐
- bzoj1040基环树
... st#include<cstdio> #include<iostream> #include<algorithm> #include<cmath> ...
- thinkphp3.2 删除缓存文件
//删除后台缓存 public function ajaxcleanCache(){ $dir = "./Application/Runtime/Logs/Admin/"; if( ...
- jQuery的下拉选select2插件用法
1转自:https://www.jb51.net/article/95561.htm 用了这么久的Select2插件,也该写篇文章总结总结.当初感觉Select2不是特别好用,但又找不到比它更好的下拉 ...
- Android CameraManager 类
先看代码: private SurfaceView mSurfaceView; private SurfaceHolder mSurfaceHolder; private CameraManager ...
- 带URL的XML解析方式
XmlDocument xml = new XmlDocument(); xml.LoadXml(responseString); XmlNode root = xml.DocumentElement ...
- Maven安装配置(Windows10)
想要安装 Apache Maven 在Windows 系统上, 需要下载 Maven 的 zip 文件,并将其解压到你想安装的目录,并配置 Windows 环境变量. 所需工具 : JDK 1.8 M ...
- log4net可视化查询
转自:https://www.cnblogs.com/huangxincheng/p/9120028.html 小步快跑的公司可以最简化操作直接通过log4net将日志写入ElasticSearch ...
- Terraria(泰拉瑞亚)存档覆盖(Linux)
这是一篇关于游戏的不正经博客 ~ 游戏介绍: <泰拉瑞亚>是由Re-Logic公司开发的一款高自由度的沙盒游戏,于2011年5月16日在PC上发行. 玩家可以在游戏中做很多事情:制造武器战 ...
- 字符串函数之Strtok()函数
Strtok()函数详解: 该函数包含在"string.h"头文件中 函数原型: char* strtok (char* str,constchar* delimiters ) ...
- 英语口语练习系列-C39-舞蹈-谈论昨天的活动
词汇-舞蹈(dancing) ballet body shaking sway the body have a good figure special training arm movement da ...