当前位置:网站首页>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
边栏推荐
- 测试流程整理(3)
- Detailed index of MySQL
- R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
- qt 怎么检测鼠标在不在某个控件上
- Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
- 2022 game going to sea practical release strategy
- Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
- R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
- Blob, text geometry or JSON column'xxx'can't have a default value query question
- 奇妙秘境 码蹄集
猜你喜欢
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
MATLAB中tiledlayout函数使用
392. Judgement subsequence
Excel快速合并多行数据
The font of markdown grammar is marked in red
Test evaluation of software testing
测试流程整理(3)
Why should Base64 encoding be used for image transmission
学内核之三:使用GDB跟踪内核调用链
Test process arrangement (3)
随机推荐
File creation, writing, reading, deletion (transfer) in go language
基于YOLOv1的口罩佩戴检测
Golang 使用 JSON unmarshal 数字到 interface{} 数字变成 float64 类型(转)
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Install and use MAC redis, connect to remote server redis
Excel quickly merges multiple rows of data
R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
Data warehouse interview question preparation
Fs4059c is a 5V input boost charging 12.6v1.2a. Inputting a small current to three lithium battery charging chips will not pull it dead. The temperature is 60 ° and 1000-1100ma is recommended
Gorm data insertion (transfer)
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
【信息检索】链接分析
Assertion of unittest framework
C # WPF realizes the real-time screen capture function of screen capture box
奇妙秘境 码蹄集
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
92.(cesium篇)cesium楼栋分层
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering