【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-> ...
随机推荐
- Windows Internals 笔记——终止进程
1.进程可以通过以下四种方式终止: 主线程的入口点函数返回(强烈推荐的方式) 进程中的一个线程调用ExitProcess函数(避免这种方式) 另一个进程中的线程调用TerminateProcess函数 ...
- ubuntu chrome 无法从该网站添加应用,拓展程序或脚本
昨天装好ubuntu 18.04 lts版本后,下载了chrome( 版本 68.0.3440.106)和SwitchyOmega,本来计划离线安装,结果提示“无法添加来自此网站的应用.扩展程序和应用 ...
- python的占位格式符 %
# 格式化输出name = "sz"age = 18# 我的名字是xxx,年龄是xxxprint("我的名字是%s,年龄是%d"%(name,age)) 这是我 ...
- Rank of Tetris 拓扑排序+并查集
Problem Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子: ...
- String与StringBuffer
转载于:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616330.htm l火之光 StringBuffer类和String一样,也用来 ...
- Ubuntu16.04 14.04安装配置Caffe(GPU版)
caffe配置过程很长啊,坑非常多,没有linux基础的估计会香菇的.我参考了网上很多的帖子,基本上每个帖子都有或多或少的问题,研究很久最终配置成功.参考过的帖子太多,都记不太清来源了.为了对前人的感 ...
- [PKUSC2018]星际穿越
[PKUSC2018]星际穿越 题目大意: 有一排编号为\(1\sim n\)的\(n(n\le3\times10^5)\)个点,第\(i(i\ge 2)\)个点与\([l_i,i-1]\)之间所有点 ...
- Linux中Hadoop的环境搭建
一:下载安装 Hadoop 1.1:下载指定的Hadoop 1.2:通过XFTP把文件上传到master电脑bigData目录下 1.3:解压hadoop压缩文件 tar -xvf hadoop-2. ...
- mysql 查询 两个表中不同字段的 和,并通过两个表的时间来分组
( SELECT sum( a.cost_sum ) AS sum_cost, sum( a.phone_sum ) AS sum_phone, sum( a.arrive_sum ) AS sum_ ...
- python中的包与模块
'''模块与模块之间的调用''' import first #调用整个变量 print(first.Index) # #调用函数 print(first.hello()) # per = first. ...