当前位置:网站首页>Leetcode 61: 旋转链表
Leetcode 61: 旋转链表
2022-07-04 12:51:00 【范谦之】
题目描述
给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。
示例1
输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]
示例2
输入:head = [0,1,2], k = 4
输出:[2,0,1]
提示
- 链表中节点的数目在范围 [0, 500] 内
- -100 <= Node.val <= 100
- 0 <= k <= 2 * 1 0 9 10^9 109
思路
由于k的值可能很大,所以要先将k除len取余。
本题的实质是找到倒数第k个结点,将其后面所有结点调到链表前面。
找到倒数第k个结点:定义两个结点,让其相隔k个结点,后一起往后移动。
代码
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
l = 0
cur = head
while cur is not None:
l+=1
cur = cur.next
k = k%l
bef = head
aft = head
for i in range(k):
aft = aft.next
while(aft.next is not None):
aft = aft.next
bef = bef.next
# print(bef.val, aft.val)
aft.next = head
res = bef.next
bef.next = None
return res
边栏推荐
- qt 怎么检测鼠标在不在某个控件上
- R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
- 吃透Chisel语言.08.Chisel基础(五)——Wire、Reg和IO,以及如何理解Chisel生成硬件
- [matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
- 基于51单片机的超声波测距仪
- Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
- [antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
- gorm 之数据插入(转)
- R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
- R language dplyr package summary_ If function calculates the mean and median of all numerical data columns in dataframe data, and summarizes all numerical variables based on conditions
猜你喜欢
Detailed index of MySQL
自主工业软件的创新与发展
【信息检索】链接分析
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
How to package QT and share exe
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
基于PaddleX的智能零售柜商品识别
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
TestSuite and testrunner in unittest
Rich text editing: wangeditor tutorial
随机推荐
Vscode common plug-ins summary
R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
China Post technology rushes to the scientific innovation board: the annual revenue is 2.058 billion, and the postal group is the major shareholder
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
Blob, text geometry or JSON column'xxx'can't have a default value query question
Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
游戏出海,全球化运营
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
基于PaddleX的智能零售柜商品识别
MySQL version 8 installation Free Tutorial
golang fmt. Printf() (turn)
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
Assertion of unittest framework
LiveData
MATLAB中tiledlayout函数使用
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Use of arouter
FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA